Android 5.0+ getRunningTasks is deprecated

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

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  taskInfo = am.getRunningTasks(5);             top = taskInfo.get(0).topActivity.getPackageName();             Log.v(TAG, "top app = " + top);         }else{ //For versions Lollipop and above             List 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.

Output for above code (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

Now how to decide com.google.android.videos is the top app from the above list programmatically?

How does applocker (or similar to applocker) app works on 5.0+? Hope someone helps which will be helpful for someone.

回答1:

So here is an update. Tested in 5.0 and 5.1.1 device. Working perfectly.

if (Build.VERSION.SDK_INT  taskInfo = am.getRunningTasks(5);             top = taskInfo.get(0).topActivity.getPackageName();             Log.v(TAG, "top app = " + top);         }else{ //For versions Lollipop and above             List processes = ProcessManager.getRunningForegroundApps(getApplicationContext());             Collections.sort(processes, new ProcessManager.ProcessComparator());             for (int i = 0; i 

Thanks to this library

Now i'm able to get foreground task in Android 5.0+



回答2:

The "apps with usage accesse" feature sometimes can meet your needs



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!