zoukankan      html  css  js  c++  java
  • android切换前后台状态监听

    public class BaseApplication extends Application {
        private static BaseApplication instance;
        /**
         * 当前Activity个数
         */
        private int activityNumber = 0;
        @Override
        public void onCreate() {
            super.onCreate();
            registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
            instance = this;
        }
    
        public static BaseApplication getInstance() {
            return instance;
        }
    
    
        /**
         * Activity 生命周期监听,用于监控app前后台状态切换
         */
        ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    
            }
    
            @Override
            public void onActivityStarted(Activity activity) {
                if (activityNumber == 0) {
                    LogUtil.i("app回到前台");
                }
                activityNumber++;
                LogUtil.i("activityNumber = " + activityNumber);
            }
    
            @Override
            public void onActivityResumed(Activity activity) {
            }
    
            @Override
            public void onActivityPaused(Activity activity) {
            }
    
            @Override
            public void onActivityStopped(Activity activity) {
                activityNumber--;
                LogUtil.i("activityNumber = " + activityNumber);
                if (activityNumber == 0) {
                    // app回到后台
                    LogUtil.i("app回到后台");
                }
            }
    
            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }
    
            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        };
    
    }
    
  • 相关阅读:
    Uva 10779 collector's problem
    poj 2728 最优比率树(最小生成树问题)
    LA 3126 二分图匹配 最小路径覆盖
    poj 1149 最大流构图
    Step By Step(Java XML篇)
    Step By Step(Java 输入输出篇)
    Step By Step(Java 集合篇)
    Step By Step(Java 线程篇)
    Step By Step(Java 反射篇)
    Step By Step(Java 国际化篇)
  • 原文地址:https://www.cnblogs.com/baiyi168/p/8252825.html
Copyright © 2011-2022 走看看