我已完成此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+,我得到所有正在运行的前台进程,但我无法断定哪个应用程序是顶级应用程序。
上述代码的输出(for else condition)
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+上运行? 希望有人帮助对某人有所帮助。
答案 0 :(得分:3)
所以这是一个更新。在5.0和5.1.1设备中测试。工作得很好。
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 (int i = 0; i <=processes.size()-1 ; i++) {
if(processes.get(i).name.equalsIgnoreCase("com.google.android.gms")) { //always the package name above/below this package is the top app
if ((i+1)<=processes.size()-1) { //If processes.get(i+1) available, then that app is the top app
top = processes.get(i + 1).name;
} else if (i!=0) { //If the last package name is "com.google.android.gms" then the package name above this is the top app
top = processes.get(i - 1).name;
} else{
if (i == processes.size()-1) { //If only one package name available
top = processes.get(i).name;
}
}
Log.v(TAG, "top app = " + top);
}
}
}
感谢library
现在我可以在Android 5.0 +中获得前台任务
答案 1 :(得分:0)
“具有使用权限的应用”功能有时可以满足您的需求