Android 前后台切换监听
class AppLifeObserver: LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onForeground() {
LogUtils.logd("Application enters the foreground")
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onBackground() {
LogUtils.logd("Application enters the background")
}
}
class AppLifeObserver: LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
when (event){
Lifecycle.Event.ON_RESUME -> {
}
else -> {}
}
}
}
再Application中调用
lifecycle.addObserver(AppLifeObserver())