[英]Android 5.0+ getRunningTasks is deprecated
I have gone through this question and this question . 我已经完成了这个问题和这个问题 。 But with the help of this library i can now get the list of foreground tasks using following code.
但是在这个库的帮助下,我现在可以使用以下代码获取前台任务列表。
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);
}
}
}
Here, for Android 5.0+, i get all running foreground process but i'm unable to conclude which app is the top app. 在这里,对于Android 5.0+,我得到所有正在运行的前台进程,但我无法断定哪个应用程序是顶级应用程序。
Output for above code (for else condition) 上述代码的输出(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
Here my top app is com.google.android.videos
这里我的热门应用是
com.google.android.videos
Now how to decide com.google.android.videos
is the top app from the above list programmatically? 现在如何以编程方式决定
com.google.android.videos
是上面列表中的顶级应用程序?
How does applocker (or similar to applocker) app works on 5.0+? applocker(或类似于applocker)应用程序如何在5.0+上运行? Hope someone helps which will be helpful for someone.
希望有人帮助对某人有所帮助。
So here is an update. 所以这是一个更新。 Tested in 5.0 and 5.1.1 device.
在5.0和5.1.1设备中测试。 Working perfectly.
工作完美。
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);
}
}
}
Thanks to this library 感谢这个图书馆
Now i'm able to get foreground task in Android 5.0+ 现在我可以在Android 5.0+中获得前台任务了
“具有使用访问权限的应用”功能有时可以满足您的需求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.