开启一个服务有两种
1.startService
生命周期变化 onCreat onStartComment onStart 再次调用startService 会调用onStartConmment onStart 一直如此,调用stopService onDestory
2.bindService
onCreate onBind 再次调用bindService 生命周期无变化调用unBindService onUnder onDestory
如果联合使用呢。
1.startService >>>onCreat.onStartComment>>onStart
2.然后bindService>>onBind
3.然后stopService 无反应
4.然后UnBindService>>UnBind>onDestory
3和4顺序换一下
3unBind
4点击后>>onDestory
startService 生命周期与开启他的调用者生命周期等无关。
bindService 生命周期与开启的调用者的销毁等一起销毁。
所以startServcie适合开启一些涉及整个app的服务。
bindService适合开启当前页面需要的服务。
还有一个onReBind。在service没有onDestory,并且unBind返回为true,就会调用这个方法。
android5.0之后,要显示开启service。
网友评论