一,service的启动方式
在讨论如何停止service之前,我们需要回顾一下启动service的两种方式。
1.1 startservice
生命周期:onCreate()->onStartCommand()->Service running->onDestroy()
1.2 bindservice
生命周期:onCreate()->onBind()->Clients are bound to service->onUnbind()->onDestroy()
二,停止service的几种场景:
2.1 单一启动方式下如何停止service
2.1.1 只调用了startservice
(1)startServic:调用onCreate()->onStartCommand()
(2)stopService:调用onDestory()
2.1.2 只调用了bindservice
(1)bindservice:调用onCreate()->onBind()
(2)unbindService:调用onUnbind()->onDestroy()
2.2 混合启动方式如何停止service
2.2.1 先stopService,再unbindService
(1)startServic:调用onCreate()->onStartCommand()
(2)bindService:调用onBind()
(3)stopService:没有调用onDestory() Service仍然在运行!
(4)unbindService:调用onUnbind()->onDestory() 此时Service关闭!
2.2.2 先unbindService,再stopService
(1)startServic:调用onCreate()->onStartCommand()
(2)bindService:调用onBind()
(3)unbindService:调用onUnbind() Service仍然在运行!
(4)stopService:调用onDestory() 此时Service才关闭!