四大组件之Service(二)-Service在各种情况下的生命周期

来源:互联网 发布:淘宝网夏秋棉麻连衣裙 编辑:程序博客网 时间:2024/06/03 16:19
更新时间 修改意见 2016-08-02 陈敏

第3节 Service的生命周期

3.1 Service的生命状态

Activity类似,Service也是有生命周期的。在实现一个Service的时候,可以覆盖它的生命周期函数,当它进入不同生命状态的时候,这些函数会被触发,我们就可以在它不同的生命阶段做不同的逻辑操作。

public class MyService extends Service {    public MyService() {    }    @Override    public IBinder onBind(Intent intent) {        return null;    }    @Override    public boolean onUnbind(Intent intent) {        return super.onUnbind(intent);    }    @Override    public void onRebind(Intent intent) {        super.onRebind(intent);    }    @Override    public void onCreate() {        super.onCreate();    }    @Override    public void onDestroy() {        super.onDestroy();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        return super.onStartCommand(intent, flags, startId);    }}

因为Service不像Activity有需要显示的界面,所以它的状态少了很多,没有onResume() onPause()等等与界面显示相关的状态。

这里面的onStartCommand()函数与onUnbind()函数要特别注意,它们的返回值有特别的意义,会影响到Service的生命周期,我们会在最后单独解释。

3.2 生命周期

通过startService()运行的Service与通过bindService()运行的Service相比较,它们的生命周期有所不同。

假设Service-MyService 还没有被启动过,我们来看看它被一个或多个组件使用startService() stopService() bindService() unbindService()这些方法后,生命周期会怎么变化。

组件名称 启动Service的方法 关闭Service的方法 A startService() stopService() B startService() stopService() C bindService() unbindService() D bindService() unbindService()

3.2.1 Start Service的生命周期

对于只使用startService()stopService()来控制Service的情况:

  1. 情况1

    A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->A -- stopService() ->MyService -- onDestroy()
  2. 情况2

    A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->A -- startService() ->MyService -- onStartCommand() ->A -- stopService() ->MyService -- onDestroy()
  3. 情况3

    A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->B -- startService() ->MyService -- onStartCommand() ->A -- stopService() ->MyService -- onDestroy()
  4. 情况4

    A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->B -- startService() ->MyService -- onStartCommand() ->B -- stopService() ->MyService -- onDestroy()

可以看出,

  1. 任意组件调用startService()后,如果这个Service还没有创建,系统会首先创建-onCreate()

  2. 之后触发onStartCommand()

  3. 任意组件调用stopService()后,如果这个Service还没有被销毁,系统会销毁-onDestroy()

3.2.2 Bind Service的生命周期

onStartCommand()函数与onUnbind()函数也会对生命周期有影响,所以这里我们假设使用的是默认的返回值,

@Overridepublic boolean onUnbind(Intent intent) {    //实际上是返回false   return super.onUnbind(intent);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {    //实际上是返回START_STICKY   return super.onStartCommand(intent, flags, startId);}

组件触发Service运行的bindService()函数也会对生命周期有影响--第三个参数,所以这里我们假设使用Context.BIND_AUTO_CREATE

bindService(i, mServiceConnection, Context.BIND_AUTO_CREATE);

那么,对于只使用bindService()unbindService()来控制Service的情况:

  1. 情况1

    C -- bindService() ->MyService -- onCreate() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->C -- unbindService() ->MyService -- onUnbind()MyService -- onDestroy()
  2. 情况2

    C -- bindService() ->MyService -- onCreate() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->C -- bindService() ->MyService -- 无变化C -- unbindService() ->MyService -- onUnbind()MyService -- onDestroy()
  3. 情况3

    C -- bindService() ->MyService -- onCreate() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->D -- bindService() ->MyService -- onBind() ->D -- onServiceConnected()被触发 ->C -- unbindService() ->MyService -- onUnbind()D -- unbindService() ->MyService -- onUnbind()MyService -- onDestroy()
  4. 情况4

    C -- bindService() ->MyService -- onCreate() ->MyService -- onBind() ->C -- onServiceConnected()`被触发 ->D -- bindService() ->MyService -- onBind() ->D -- onServiceConnected()被触发 ->D -- unbindService() ->MyService -- onUnbind()C -- unbindService() ->MyService -- onUnbind()MyService -- onDestroy()

可以看出,

  1. 任意组件调用bindService()后,如果这个Service还没有创建,系统会首先创建-onCreate()

  2. 之后触发onBind()

  3. 之后组件的onServiceConnected()被触发,告知绑定成功;

  4. 任意已绑定的组件调用unbindService()后,会触发onUnbind()

  5. 如果这个Service没有与其他的组件绑定,那么系统会销毁-onDestroy()

3.2.3 混合模式下的生命周期

对于使用了startService() stopService()bindService()unbindService()等两种方式来控制Service的情况:

  1. 情况1

    A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->C -- bindService() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->C -- unbindService() ->MyService -- onUnbind()A -- stopService() ->MyService -- onDestroy()
  2. 情况2

    A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->C -- bindService() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->A -- stopService() ->MyService -- 无变化C -- unbindService() ->MyService -- onUnbind()MyService -- onDestroy()
  3. 情况3

    C -- bindService() ->MyService -- onCreate() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->A -- startService() ->MyService -- onStartCommand() ->C -- unbindService() ->MyService -- onUnbind()A -- stopService() ->MyService -- onDestroy()
  4. 情况4

    C -- bindService() ->MyService -- onCreate() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->A -- startService() ->MyService -- onStartCommand() ->A -- stopService() ->MyService -- 无变化C -- unbindService() ->MyService -- onUnbind()MyService -- onDestroy()

可以看出,当一个Service被创建-onCreate()之后;如果任意组件想要销毁-onDestroy()这个Service;必须等到Service与它关联的所有组件切断联系(通过stopService()或者unbindService())才行。

3.3 其它因素对生命周期的影响

Service生命周期的影响还有其它的因素。

3.3.1 unbind()函数返回值

自定义Service时,unbind()函数的返回值对Service的生命周期是有影响的。它会决定onRebind()函数是否被调用。

前面我们都看到了在unbind()返回falseService生命周期的变化。在unbind()返回true时,它的周期变化如下,

A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->C -- bindService() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->C -- unbindService() ->MyService -- onUnbind()C -- bindService() ->MyService -- onRebind() ->C -- onServiceConnected()被触发 ->    ......

也就是说,假如一个Service正在运行,此时有个组件C要绑定它,那么会触发onBind()函数;如果C解除绑定,然后又再次绑定,那么不会触发onBind()而是触发onRebind()函数。

3.3.2 bindService函数的参数

在绑定Service的时候,通常使用的Context.BIND_AUTO_CREATE参数,

bindService(i, mServiceConnection, Context.BIND_AUTO_CREATE);

如果不使用Context.BIND_AUTO_CREATE,那么一个Service在没有被运行起来之前,使用bindService()是不会让Service自动运行起来的。

但是如果设置上了这个参数,那么就会让它运行起来,然后进行绑定。

除了Context.BIND_AUTO_CREATE参数,还有好些参数可以使用,

BIND_NOT_FOREGROUND,BIND_ABOVE_CLIENT,BIND_ALLOW_OOM_MANAGEMENT,BIND_WAIVE_PRIORITY,BIND_IMPORTANT,BIND_ADJUST_WITH_ACTIVITY

可以用的方式同时使用其它的标志,例如,

bindService(i, mServiceConnection, Context.BIND_AUTO_CREATE|Context.BIND_NOT_FOREGROUND);

3.3.3 onStartCommand函数返回值

前面的onStartCommand()返回值使用的是START_STICKY,它对Service的周期都是有影响的。

Service启动之后,如果因为某种意外而停止运行(例如系统回收了该Service),那么系统要自动的把这个Service再运行起来。这个时候,onStartCommand()的返回值就决定了Service重启的行为,

  1. START_STICKYService重启(onCreate())之后,会触发onStartCommand(),但是此时onStartCommand()的传入的参数Intent会变成空值,

    public class MyService extends Service {    public MyService() {    }    ......    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        if(intent == null) {            //因为这个Service在onStartCommand()返回的是START_STICKY,所以异常退出后,传入的`intent`是空值        }        return START_STICKY;    }}
  2. START_NOT_STICKYService将不会被自动重启,所以也就不会触发onStartCommand()

  3. START_REDELIVER_INTENTService重启(onCreate())之后,会触发onStartCommand(),此时onStartCommand()的参数Intent不会变成空值。该Intent将是以前想要启动这个ServiceIntent;如果以前有多个Intent想要启动它,那么这里会传入最后一个,也就是最近的一个;


/*******************************************************************/
* 版权声明
* 本教程只在CSDN和安豆网发布,其他网站出现本教程均属侵权。

*另外,我们还推出了Arduino智能硬件相关的教程,您可以在我们的网店跟我学Arduino编程中购买相关硬件。同时也感谢大家对我们这些码农的支持。

*最后再次感谢各位读者对安豆的支持,谢谢:)
/*******************************************************************/

2 0
原创粉丝点击