我的 Android 应用程序中有这个代码片段,用于检查应用程序是否被杀死。
static boolean isAppKilled(Context appContext) {
boolean appProcessRunning = false;
ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses != null) {
String packageName = appContext.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
System.out.println("LM:appProcess: " + appProcess.processName);
System.out.println("LM:appProcess: " + appProcess.importance);
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE &&
appProcess.processName.equals(packageName)) {
appProcessRunning = true;
break;
}
}
}
return appProcessRunning;
}
但是,这个片段也为后台的应用程序返回“true”,这不是有意的。
你们中的任何人都可以为我提供一个代码片段来满足上述所有情况吗?