废话少说,直接上代码
private boolean isBackground() {
ActivityManager activityManager = (ActivityManager) kaibesApplication
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(kaibesApplication
.getPackageName())
&& (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_SERVICE || appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND)) {
return true;
}
}
return false;
}