一、LifecycleService的用途?

  解耦系统组件Service的生命周期。

  LifecycleService继承自Service。

二、示例

  MyServiceObserver类:



class MyServiceObserver : LifecycleObserver {

}


  MyService类:



class MyService : LifecycleService() {

private val _observer: MyServiceObserver = MyServiceObserver()

init {
lifecycle.addObserver(_observer)
}

override fun onDestroy() {
super.onDestroy()

lifecycle.removeObserver(_observer)
}

}