判断程序是否处于后台的方法

来源:互联网 发布:破解版java游戏大全 编辑:程序博客网 时间:2024/06/05 12:41
    public static boolean isBackground(Context context) {
        boolean isBackground = false;
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> 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
                 */
                GioneeLog.debug(TAG, "isBackground appProcess.importance=" + appProcess.importance);
                if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    isBackground = true;
                    break;
                }
            }
        }
        GioneeLog.debug(TAG, "isBackground isBackground=" + isBackground);
        return isBackground;
    }
0 0
原创粉丝点击