四大组件之Service(二)-Service在各种情况下的生命周期
来源:互联网 发布:淘宝网夏秋棉麻连衣裙 编辑:程序博客网 时间:2024/06/03 16:19
第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()
这些方法后,生命周期会怎么变化。
3.2.1 Start Service的生命周期
对于只使用startService()
和stopService()
来控制Service
的情况:
情况1
A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->A -- stopService() ->MyService -- onDestroy()
情况2
A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->A -- startService() ->MyService -- onStartCommand() ->A -- stopService() ->MyService -- onDestroy()
情况3
A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->B -- startService() ->MyService -- onStartCommand() ->A -- stopService() ->MyService -- onDestroy()
情况4
A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->B -- startService() ->MyService -- onStartCommand() ->B -- stopService() ->MyService -- onDestroy()
可以看出,
任意组件调用
startService()
后,如果这个Service
还没有创建,系统会首先创建-onCreate()
;之后触发
onStartCommand()
;任意组件调用
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
C -- bindService() ->MyService -- onCreate() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->C -- unbindService() ->MyService -- onUnbind()MyService -- onDestroy()
情况2
C -- bindService() ->MyService -- onCreate() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->C -- bindService() ->MyService -- 无变化C -- unbindService() ->MyService -- onUnbind()MyService -- onDestroy()
情况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
C -- bindService() ->MyService -- onCreate() ->MyService -- onBind() ->C -- onServiceConnected()`被触发 ->D -- bindService() ->MyService -- onBind() ->D -- onServiceConnected()被触发 ->D -- unbindService() ->MyService -- onUnbind()C -- unbindService() ->MyService -- onUnbind()MyService -- onDestroy()
可以看出,
任意组件调用
bindService()
后,如果这个Service
还没有创建,系统会首先创建-onCreate()
;之后触发
onBind()
;之后组件的
onServiceConnected()
被触发,告知绑定成功;任意已绑定的组件调用
unbindService()
后,会触发onUnbind()
;如果这个
Service
没有与其他的组件绑定,那么系统会销毁-onDestroy()
;
3.2.3 混合模式下的生命周期
对于使用了startService()
stopService()
与bindService()
和unbindService()
等两种方式来控制Service
的情况:
情况1
A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->C -- bindService() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->C -- unbindService() ->MyService -- onUnbind()A -- stopService() ->MyService -- onDestroy()
情况2
A -- startService() ->MyService -- onCreate() ->MyService -- onStartCommand() ->C -- bindService() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->A -- stopService() ->MyService -- 无变化C -- unbindService() ->MyService -- onUnbind()MyService -- onDestroy()
情况3
C -- bindService() ->MyService -- onCreate() ->MyService -- onBind() ->C -- onServiceConnected()被触发 ->A -- startService() ->MyService -- onStartCommand() ->C -- unbindService() ->MyService -- onUnbind()A -- stopService() ->MyService -- onDestroy()
情况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()
返回false
时Service
生命周期的变化。在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
重启的行为,
START_STICKY
:Service
重启(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; }}
START_NOT_STICKY
:Service
将不会被自动重启,所以也就不会触发onStartCommand()
;START_REDELIVER_INTENT
:Service
重启(onCreate())之后,会触发onStartCommand()
,此时onStartCommand()
的参数Intent
不会变成空值。该Intent
将是以前想要启动这个Service
的Intent
;如果以前有多个Intent
想要启动它,那么这里会传入最后一个,也就是最近的一个;
/*******************************************************************/
* 版权声明
* 本教程只在CSDN和安豆网发布,其他网站出现本教程均属侵权。
*另外,我们还推出了Arduino智能硬件相关的教程,您可以在我们的网店跟我学Arduino编程中购买相关硬件。同时也感谢大家对我们这些码农的支持。
*最后再次感谢各位读者对安豆
的支持,谢谢:)
/*******************************************************************/
- 四大组件之Service(二)-Service在各种情况下的生命周期
- Android 四大组件之Service 的生命周期与启动Service
- 四大组件之Service(二)
- Android 四大组件之Service 生命周期
- 四大组件之Service 生命周期(3)
- Android 四大组件之Service 的生命周期和使用
- Android Studio 四大组件之 Service的生命周期上
- Android 四大组件之Service 的生命周期和使用
- Android四大组件之Service的两种使用情况
- 四大组件---Service的生命周期(一)
- Android四大组件之Service第一章:service及其生命周期
- Android 四大组件生命周期:Service
- 四大组件之Service(一)-双子座的Service-Start Service与Bind Service
- android 四大组件之--------------Service <二>
- Android 四大组件之 Service (二)
- Android四大组件之Service(二)
- Android四大组件之二:Service
- Android 四大组件学习之Service二
- 怎样在Eclipse中快速查看各种源代码?
- 鼠标双击
- 基数排序
- 月薪3万的程序员告诉你:这样工作才能拿高薪
- Android有效地减少方法数
- 四大组件之Service(二)-Service在各种情况下的生命周期
- 堆排序
- Android LayoutInflater详解
- 桶排序
- 第十.十一周项目3 - 警察和厨师——2
- Unity3D内部串口通信和Unity3D与Winform程序的串口通信的实现和异常问题
- 怎样在Eclipse中快速查阅各种api/docs文档?|
- AOPR软件怎么更改Excel VBA密码
- 第十三周项目2-动物这样叫——1