通过实现LifecycleEventObserver接口,打印应用Application的生命周期

通过实现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;
        }
    }
}
  1. 应用启动
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
  1. 在通知栏打开其他应用
onStateChanged(line:182):[LifecycleChecker] - event: ON_PAUSE
onStateChanged(line:182):[LifecycleChecker] - event: ON_STOP
onStateChanged(line:187):[LifecycleChecker]: app moved to background
  1. 关闭其他应用
onStateChanged(line:182):[LifecycleChecker] - event: ON_START
onStateChanged(line:184):[LifecycleChecker]: app moved to foreground
onStateChanged(line:182):[LifecycleChecker] - event: ON_RESUME
  1. 按Home键退到后台、按返回键退出应用、按多任务键时
onStateChanged(line:182):[LifecycleChecker] - event: ON_PAUSE
onStateChanged(line:182):[LifecycleChecker] - event: ON_STOP
onStateChanged(line:187):[LifecycleChecker]: app moved to background
  1. 按多任务返回前台、点桌面应用图标
onStateChanged(line:182):[LifecycleChecker] - event: ON_START
onStateChanged(line:184):[LifecycleChecker]: app moved to foreground
onStateChanged(line:182):[LifecycleChecker] - event: ON_RESUME
  1. 杀掉应用:无打印信息
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值