如何获取当前正在运行的所有任务 [英] How to get all the tasks that are running currently

查看:92
本文介绍了如何获取当前正在运行的所有任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取所有正在android中运行的任务.我在ActivityManager中找到了getRunningTasks,但是从android-5.0开始,getRunningTasks可能无法提供所有任务(在我的情况下,它提供了主屏幕和我的应用程序).

I want to get all the tasks that are running in android.I found getRunningTasks in ActivityManager but from android-5.0 the getRunningTasks may not give all the tasks (in my case it's give the home screen and my application).

此外,是否可以找到用户当前正在与之交互的任务?

In addition is it possible to find the task that the user is currently interacting with?

任何帮助将不胜感激.

谢谢.

编辑:我使用的是以下代码.但这只给了我一个过程(这就是我的应用程序).即使我打开其他应用程序也没有显示它们.一加2.

Edit: I am using the following piece of code.But it's giving me only one process (that's my app).Even if i open other apps it's not showing them.I am running this on One Plus2.

@Override
    protected void onHandleIntent(Intent intent) {
        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                getRunningApps();
            }
        }, 0, 1000);//put here time 1000 milliseconds=1 second
    }

    public void getRunningApps() {
        final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        final List<ActivityManager.RunningAppProcessInfo> recentTasks = activityManager.getRunningAppProcesses();

        for (int i = 0; i < recentTasks.size(); i++)
        {
            Log.d("Executed app", "Application executed : " +recentTasks.get(i).pkgList[0]+ "\t\t ID: "+recentTasks.get(i).pid+"");
        }
    }

推荐答案

使用 getRunningAppProcesses ,这将返回 RunningAppProcessInfo 对象的列表,这些对象将为您提供有关每个对象的一些数据正在运行的应用程序.您可以获取的有关该应用程序的信息之一是其 importance (重要性),其值可以为 IMPORTANCE_FOREGROUND ,表示该进程是当前活动的应用程序!

Use getRunningAppProcesses, which will return a list of RunningAppProcessInfo objects that will give you some data about each running app. One of the pieces of information you can retrieve about the app is its importance, which can have a value IMPORTANCE_FOREGROUND which indicates that the process is the currently active application!

相关文档:

getRunningAppProcesses

RunningAppProcessInfo

IMPORTANCE_FOREGROUND

如果这对您有帮助,请随时按向左的箭头:)

If this helped you, feel free to press that arrow to the left :)

这篇关于如何获取当前正在运行的所有任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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