[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BMO4QjBH-1637123951461)(https://user-gold-cdn.xitu.io/2019/11/3/16e2f2db165413d9?imageView2/0/w/1280/h/960/ignore-error/1)]
2.1 Service 的生命周期
- 我们先来看看
Service
的生命周期 的基本流程 - 一张闻名遐迩的图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XpgAeSHX-1637123951511)(https://user-gold-cdn.xitu.io/2019/11/3/16e2f2dcb42f7251?imageView2/0/w/1280/h/960/ignore-error/1)]
2.2 开启 Service 的两种方式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BHGBSTmk-1637123951512)(https://user-gold-cdn.xitu.io/2019/11/3/16e2f2dcb4682125?imageView2/0/w/1280/h/960/ignore-error/1)]
2.2.1 startService()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-waWgLpXH-1637123951513)(https://user-gold-cdn.xitu.io/2019/11/3/16e2f2dcb4bf8fdb?imageView2/0/w/1280/h/960/ignore-error/1)]
-
定义一个类继承
Service
-
在
Manifest.xml
文件中配置该Service
-
使用
Context
的startService(intent)
方法开启服务。 -
使用
Context
的stopService(intent)
方法关闭服务。 -
该启动方式,
app
杀死、Activity
销毁没有任何影响,服务不会停止销毁。
2.2.2 bindService()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SLjpdand-1637123951514)(https://user-gold-cdn.xitu.io/2019/11/3/16e2f2dcb4c7f792?imageView2/0/w/1280/h/960/ignore-error/1)]
-
创建
BindService
服务端,继承Service
并在类中,创建一个实现IBinder
接口的实例对象,并提供公共方法给客户端(Activity
)调用。 -
从
onBinder()
回调方法返回该Binder
实例。 -
在客户端(
Activity
)中, 从onServiceConnection()
回调方法参数中接收Binder
,通过Binder
对象即可访问Service
内部的数据。 -
在
manifests
中注册BindService
, 在客户端中调用bindService()
方法开启绑定Service
, 调用unbindService()
方法注销解绑Service
。 -
该启动方式依赖于客户端生命周期,当客户端
Activity
销毁时, 没有调用unbindService()
方法 ,Service
也会停止销毁。
2.3 Service 有哪些启动方法,有什么区别,怎样停用 Service
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-siIRiZC2-1637123951515)(https://user-gold-cdn.xitu.io/2019/11/3/16e2f2dcb4dc73c3?imageView2/0/w/1280/h/960/ignore-error/1)]
-
在
Service
的生命周期中,被回调的方法比Activity
少一些,只有onCreate
,onStart
,onDestroy
,onBind
和onUnbind
。 -
通常有两种方式启动一个
Service
, 他们对Service
生命周期的影响是不一样的。
2.3.1 通过 startService
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZTVhngvz-1637123951516)(https://user-gold-cdn.xitu.io/2019/11/3/16e2f2dcb819e4d5?imageView2/0/w/1280/h/960/ignore-error/1)]
Service
会经历onCreate
到onStart
,然后处于运行状态,stopService
的时候调用onDestroy
方法。
如果是调用者自己直接退出而没有调用
stopService
的话,Service
会一直在后台运行。
2.3.2 通过 bindService
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nvTE3yqa-1637123951516)(https://user-gold-cdn.xitu.io/2019/11/3/16e2f2dce02e174e?imageView2/0/w/1280/h/960/ignore-error/1)]
Service
会运行 onCreate
,然后是调用 onBind
, 这个时候调用者和 Service
绑定在一起。调用者退出了,Srevice
就会调用 onUnbind
-> onDestroyed
方法。
所谓绑定在一起就共存亡了。调用者也可以通过调用
unbindService
方法来停止服务,这时候Srevice
就会调用onUnbind
->onDestroyed
方法。
2.3.3 需要注意的是如果这几个方法交织在一起的话,会出现什么情况呢?
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4SYrS1aW-1637123951517)(https://user-gold-cdn.xitu.io/2019/11/3/16e2f2dce1301175?imageView2/0/w/1280/h/960/ignore-error/1)]
-
一个原则是
Service
的onCreate
的方法只会被调用一次,就是你无论多少次的startService
又bindService
,Service
只被创建一次。 -
如果先是
bind
了,那么start
的时候就直接运行Service
的onStart
方法,如果先是start
,那么bind
的时候就直接运行onBind
方法。 -
如果
service
运行期间调用了bindService
,这时候再调用stopService
的话,service
是不会调用onDestroy
方法的,service
就stop
不掉了,只能调用UnbindService
,service
就会被销毁 -
如果一个
service
通过startService
被start
之后,多次调用sta