Android判断当前应用是否在后台运行 Android编程判断当前应用是否在后台运行的方法示例
迟做总比不做强 人气:0想了解Android编程判断当前应用是否在后台运行的方法示例的相关内容吗,迟做总比不做强在本文为您仔细讲解Android判断当前应用是否在后台运行的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,判断,当前应用,后台运行,下面大家一起来学习吧。
本文实例讲述了Android编程判断当前应用是否在后台运行的方法。分享给大家供大家参考,具体如下:
/** 判断程序是否在后台运行 */ public static boolean isRunBackground(Context context) { ActivityManager activityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> appProcesses = activityManager .getRunningAppProcesses(); for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(context.getPackageName())) { if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) { // 表明程序在后台运行 return true; } else { return false; } } } return false; } /** 判断程序是否在前台运行(当前运行的程序) */ public boolean isRunForeground() { ActivityManager activityManager = (ActivityManager) getApplicationContext() .getSystemService(Context.ACTIVITY_SERVICE); String packageName = getApplicationContext().getPackageName(); List<RunningAppProcessInfo> appProcesses = activityManager .getRunningAppProcesses(); if (appProcesses == null) return false; for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true;// 程序运行在前台 } } return false; }
希望本文所述对大家Android程序设计有所帮助。
加载全部内容