我已经经历了这个question和这个question。但在这个library的帮助下,我现在可以使用下面的代码获取前台任务列表。Android 5.0+ getRunningTasks已弃用
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //For versions less than lollipop
ActivityManager am = ((ActivityManager) getSystemService(ACTIVITY_SERVICE));
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(5);
top = taskInfo.get(0).topActivity.getPackageName();
Log.v(TAG, "top app = " + top);
}else{ //For versions Lollipop and above
List<AndroidAppProcess> processes = ProcessManager.getRunningForegroundApps(getApplicationContext());
Collections.sort(processes, new ProcessManager.ProcessComparator());
for (AndroidAppProcess process : processes) {
if (process.foreground) {
top =process.name;
Log.v(TAG,top);
}
}
}
这里,Android 5.0以上版本,我得到所有正在运行的前台进程,但我无法断定哪个应用是顶级的应用程序。
输出上面代码(其他条件)
com.android.vending
com.google.android.gms
com.google.android.googlequicksearchbox
com.google.android.videos
com.test1
com.naag.testing
com.example.android.gettask
这里我顶程序是com.google.android.videos
现在如何决定com.google.android.videos
是从程序上面的列表顶部的应用程序?
applocker(或类似applocker)应用程序如何在5.0+上工作? 希望有人帮助,这将有助于某人。
你需要什么使一个过程最高的工艺,如CPU,内存设定条件为用法等等,我想你必须遍历每个进程提供的'statm'来找出结果。 – asgs