美文网首页
android 判断app是否在后台运行

android 判断app是否在后台运行

作者: S晔枫 | 来源:发表于2017-09-19 15:55 被阅读14次
// 判断程序是否在后台
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;
}

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

相关文章

网友评论

      本文标题:android 判断app是否在后台运行

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