黑马程序员安卓教程:Service 的生命周期回调函数

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Service 的生命周期回调函数

和Activity 一样,service 也有一系列的生命周期回调函数,你可以实现它们来监测service 状态的变化,并且在适当的时候执行适当的工作。

Like an activity, a service has lifecycle callback methods that you can implement to monitor changes in the service's state and perform work at the appropriate times. The following skeleton service demonstrates each of the lifecycle methods:

13

下面的service展示了每一个生命周期的方法:

【文件1-5】Service生命周期回调函数

1.public class TestService extends Service {

2.int mStartMode; //indicates how to behave if the service is killed

3.IBinder mBinder;// interface for clients that bind

4.boolean mAllowRebind;// indicates whether onRebind should be used

5.

6.@Override

7.public void onCreate() {

8.// The service is being created

9.}

10.

11.@Override

12.public int onStartCommand(Intent intent, int flags,int startId){

13.// The service is starting, due to a call to startService()

14.return mStartMode;

15.}

16.

17.@Override

18.public IBinder onBind(Intent intent) {

19.// A client is binding to the service with bindService()

20.return mBinder;

21.}

22.

23.@Override

24.public boolean onUnbind(Intent intent) {

25.// All clients have unbound with unbindService()

26.return mAllowRebind;

27.}

28.

29.@Override

30.public void onRebind(Intent intent){

31.// A client is binding to the service with bindService(),

32.// after onUnbind() has already been called

33.}

34.

35.@Override

36.public void onDestroy(){

37.// The service is no longer used and is being destroyed

38.}

39.}

注意:不同于Activity的生命周期回调方法,Service不须调用父类的生命周期回调方法。

Unlike the activity lifecycle callback methods,you are not required to call the superclass implementation of these callback methods.

14

相关文档
最新文档