美文网首页
ProcessLifecycleOwner判断Android应用

ProcessLifecycleOwner判断Android应用

作者: 内卷程序员 | 来源:发表于2021-08-09 23:45 被阅读0次

    构建观察者

    public class LifecyclerChecker implements LifecycleObserver{
    
        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        private void onAppBackground() {
            // 应用进入后台
            Log.e("test","LifecycleChecker onAppBackground ON_STOP");
        }
     
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        private void onAppForeground() {
            // 应用进入前台
            Log.e("test","LifecycleChecker onAppForeground ON_START");
        } 
    }
    
    

    调用

    //无论是在Application的onCreate中调用,还是在Activity的onCreate或者其他的生命周期函数中调用都可以,这样就可以获取应用是在前台还是后台。
     ProcessLifecycleOwner.get().getLifecycle().addObserver(new LifecyclerChecker());
    

    相关文章

      网友评论

          本文标题:ProcessLifecycleOwner判断Android应用

          本文链接:https://www.haomeiwen.com/subject/zfotbltx.html