android 判断app是否在后台运行

2017-09-19  本文已影响14人  S晔枫
// 判断程序是否在后台
public static boolean isBackground(Context context) {
    ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
            .getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            } else {
                return false;
            }
        }
    }
    return false;
}

可直接拿来封装成工具类 .

上一篇下一篇

猜你喜欢

热点阅读