不推荐使用Android 5.0及更高版本的getRunningTasks [英] Android 5.0+ getRunningTasks is deprecated

查看:537
本文介绍了不推荐使用Android 5.0及更高版本的getRunningTasks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历过问题 .但是借助这个,我现在可以使用以下代码获取前台任务列表.

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);
                }
            }
        }

在这里,对于Android 5.0及更高版本,我可以运行所有正在运行的前台进程,但无法确定哪个应用程序是顶级应用程序.

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

在这里,我最喜欢的应用是com.google.android.videos

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?

applocker(或类似于applocker)应用程序如何在5.0及更高版本上工作? 希望有人帮忙,这将对某人有所帮助.

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

推荐答案

所以这是一个更新.在5.0和5.1.1设备中测试.完美地工作.

So here is an update. Tested in 5.0 and 5.1.1 device. 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);
                }
            }
        }

感谢此

现在我可以在Android 5.0及更高版本中获得前台任务

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

这篇关于不推荐使用Android 5.0及更高版本的getRunningTasks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆