Android 判断app是否在前台运行

Android 判断app是否在前台运行_第1张图片
方法一:使用ActivityLifecycleCallbacks

android 在从sdk14 开始为我们提供了一个新的api 用于检测app 中activity 的生命周期,我们可以直接使用。

public interface ActivityLifecycleCallbacks {
        void onActivityCreated(Activity activity, Bundle savedInstanceState);
        void onActivityStarted(Activity activity);
        void onActivityResumed(Activity activity);
        void onActivityPaused(Activity activity);
        void onActivityStopped(Activity activity);
        void onActivitySaveInstanceState(Activity activity, Bundle outState);
        void onActivityDestroyed(Activity activity);
    }

根据 Activity 的生命周期,知道onStart() 一般表示一个Activity 处于前台,onStop() 一般表示Activity在后台不可见。所以我们设置一个int refCount 变量, onActivityStarted 的时候refCount++, onActivityStopped的时候refCount--。当onActivityStopped之后如果refCount == 0 了。那么表示我们的app里面没有处于onStart的Activity 了,
也就相当于 app 已经处于后台了。

class ActivityLifecycleListener implements ActivityLifecycleCallbacks{

        private int refCount = 0;

        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {
            refCount++;
        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {
            refCount--;
            if(refCount == 0){
                setAppGoBackGround(true);
            }
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    }

然后在app 里面的 Application onCreate()方法注册即可:

registerActivityLifecycleCallbacks(new ActivityLifecycleListener());
方法二:
public static boolean isBackground(Context context) {  
        ActivityManager activityManager = (ActivityManager) context  
                .getSystemService(Context.ACTIVITY_SERVICE);  
        List appProcesses = activityManager  
                .getRunningAppProcesses();  
        for (RunningAppProcessInfo appProcess : appProcesses) {  
            if (appProcess.processName.equals(context.getPackageName())) {  
                /* 
                BACKGROUND=400 EMPTY=500 FOREGROUND=100 
                GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200 
                 */  
                Log.i(context.getPackageName(), "此appimportace ="  
                        + appProcess.importance  
                        + ",context.getClass().getName()="  
                        + context.getClass().getName());  
                if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {  
                    Log.i(context.getPackageName(), "处于后台"  
                            + appProcess.processName);  
                    return true;  
                } else {  
                    Log.i(context.getPackageName(), "处于前台"  
                            + appProcess.processName);  
                    return false;  
                }  
            }  
        }  
        return false;  
    }  

你可能感兴趣的:(Android 判断app是否在前台运行)