【发布时间】:2015-11-25 14:03:48
【问题描述】:
我经历过这个question 和这个question。但是在library 的帮助下,我现在可以使用以下代码获取前台任务列表。
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+ 上运行? 希望有人帮助,这将对某人有所帮助。
【问题讨论】:
-
您需要设置标准来确定是什么构成了 Process Top 进程,例如 CPU、内存使用情况等,我想您必须遍历每个进程提供的
statm找出答案的过程。
标签: android android-5.0-lollipop deprecated android-6.0-marshmallow