通过实现LifecycleEventObserver接口,打印应用Application的生命周期
在Application中实现下面方法
private static boolean isAppForeground = true;
public Activity getCurrentActivity() {
return mCurrentActivity;
}
/**
* App是否在前台。
*
* @return true:在前台。
*/
public static boolean isAppForeground() {
return isAppForeground;
}
/**
* App前后台监听。
*/
private static class ApplicationObserver implements LifecycleEventObserver {
@Override
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
LogUtil.d("[LifecycleChecker] - event: "+event.name());
if (event == ON_START) {
LogUtil.d("[LifecycleChecker]: app moved to foreground");
isAppForeground = true;
} else if (event == ON_STOP) {
LogUtil.d("[LifecycleChecker]: app moved to background");
isAppForeground = false;
}
}
}
- 应用启动
onStateChanged(line:182):[LifecycleChecker] - event: ON_CREATE
onStateChanged(line:182):[LifecycleChecker] - event: ON_START
onStateChanged(line:184):[LifecycleChecker]: app moved to foregroun
onStateChanged(line:182):[LifecycleChecker] - event: ON_RESUME
- 在通知栏打开其他应用
onStateChanged(line:182):[LifecycleChecker] - event: ON_PAUSE
onStateChanged(line:182):[LifecycleChecker] - event: ON_STOP
onStateChanged(line:187):[LifecycleChecker]: app moved to background
- 关闭其他应用
onStateChanged(line:182):[LifecycleChecker] - event: ON_START
onStateChanged(line:184):[LifecycleChecker]: app moved to foreground
onStateChanged(line:182):[LifecycleChecker] - event: ON_RESUME
- 按Home键退到后台、按返回键退出应用、按多任务键时
onStateChanged(line:182):[LifecycleChecker] - event: ON_PAUSE
onStateChanged(line:182):[LifecycleChecker] - event: ON_STOP
onStateChanged(line:187):[LifecycleChecker]: app moved to background
- 按多任务返回前台、点桌面应用图标
onStateChanged(line:182):[LifecycleChecker] - event: ON_START
onStateChanged(line:184):[LifecycleChecker]: app moved to foreground
onStateChanged(line:182):[LifecycleChecker] - event: ON_RESUME
- 杀掉应用:无打印信息