/**
* 判断程序是否在后台运行
*
* @param activity
* @return true 表示在后台运行
*/
public static boolean isRunBackground(Activity activity) {
ActivityManager activityManager = (ActivityManager) activity.getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
String packageName = activity.getApplicationContext().getPackageName();
//获取Android设备中所有正在运行的App
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
if (appProcesses == null)
return true;
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
// The name of the process that this object is associated with.
if (appProcess.processName.equals(packageName)
&& appProcess.importance ==
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return false;
}
}
return true;
}
记下来 以后好复制
网友评论