Service之startService,bindService交叉使用时的生命周期

1.仅startService和stopService配合使用,与仅bindService,unbindService配合使用时,service生命周期是比较明确的

2.当startService,stopService,bindService,unbindService时,情况稍微复杂,但是通过打log可以清晰看到其中的调用过程,总结如下:

(1)针对startService和bindService:

a.先startService,则调用onCreate,onStartCommand;当bindService时,再调用onBind(不会再调用onCreate)

b.先bindServcie,则调用onCreate,onBind;当startService时,再调用onStartCommand

(2)针对stopService和unbindService:

a.先stopService,则不会调用任何方法,然后当unbindService时,依次调用onUnbind和onDestroy方法

b.先unbindService,则会调用onUnbind,然后当stopService时,会调用onDestroy


详细的log记录如下,其中,MyService中定义MyServiceBinder类(继承自IBinder),该类中定义了start方法(当Activity与service建立连接时,onServiceConnected中调用),end方法(当连接异常断开时,onServiceDisconnected中调用):

1.StartService

(1)start
12-02 07:39:50.986 24755-24755/com.example.wtx.service D/MyService: onCreate
12-02 07:39:50.986 24755-24755/com.example.wtx.service D/MyService: onStartCommand
(2)stop
12-02 07:40:31.731 24755-24755/com.example.wtx.service D/MyService:  onDestroy

2.BindService
(1)bind
12-02 07:42:12.212 24755-24755/com.example.wtx.service D/MyService: onCreate
12-02 07:42:12.212 24755-24755/com.example.wtx.service D/MyService: onBind
12-02 07:42:12.226 24755-24755/com.example.wtx.service D/MyService: MyServiceBinder.start
(2)unbind
12-02 07:42:25.559 24755-24755/com.example.wtx.service D/MyService:  onUnbind
12-02 07:42:25.559 24755-24755/com.example.wtx.service D/MyService:  onDestroy

3.先Start再bind
(1)start
12-02 07:44:34.809 24755-24755/com.example.wtx.service D/MyService: onCreate
12-02 07:44:34.809 24755-24755/com.example.wtx.service D/MyService: onStartCommand
(2)bind
12-02 07:44:47.059 24755-24755/com.example.wtx.service D/MyService: onBind
12-02 07:44:47.075 24755-24755/com.example.wtx.service D/MyService: MyServiceBinder.start
(3)stop
无新log打印
(4)unbind
12-02 07:45:19.796 24755-24755/com.example.wtx.service D/MyService: onUnbind
12-02 07:45:19.796 24755-24755/com.example.wtx.service D/MyService: onDestroy
4. 先Start再bind
(1)start
12-02 07:46:05.332 24755-24755/com.example.wtx.service D/MyService: onCreate
12-02 07:46:05.332 24755-24755/com.example.wtx.service D/MyService: onStartCommand
(2)bind
12-02 07:46:06.395 24755-24755/com.example.wtx.service D/MyService: onBind
12-02 07:46:06.409 24755-24755/com.example.wtx.service D/MyService: MyServiceBinder.start
(3)unbind
12-02 07:46:18.284 24755-24755/com.example.wtx.service D/MyService: onUnbind
(4)stop
12-02 07:46:56.315 24755-24755/com.example.wtx.service D/MyService: onDestroy

5.先Bind再start
(1)bind
12-02 07:48:04.821 24755-24755/com.example.wtx.service D/MyService: onCreate
12-02 07:48:04.821 24755-24755/com.example.wtx.service D/MyService: onBind
12-02 07:48:04.832 24755-24755/com.example.wtx.service D/MyService: MyServiceBinder.start
(2)start
12-02 07:48:18.636 24755-24755/com.example.wtx.service D/MyService: onStartCommand
(3)unbind
12-02 07:48:34.132 24755-24755/com.example.wtx.service D/MyService: onUnbind
(4)stop
12-02 07:48:45.197 24755-24755/com.example.wtx.service D/MyService: onDestroy
6.先Bind再start
(1)bind
12-02 07:51:01.932 11090-11090/com.example.wtx.service D/MyService: onCreate
12-02 07:51:01.932 11090-11090/com.example.wtx.service D/MyService: onBind
12-02 07:51:01.944 11090-11090/com.example.wtx.service D/MyService: MyServiceBinder.start
(2)start
12-02 07:51:09.613 11090-11090/com.example.wtx.service D/MyService: onStartCommand
(3)stop
无新log打印
(4)unbind
12-02 07:51:23.592 11090-11090/com.example.wtx.service D/MyService: onUnbind
12-02 07:51:23.592 11090-11090/com.example.wtx.service D/MyService: onDestroy

你可能感兴趣的:(Service之startService,bindService交叉使用时的生命周期)