How to get all the tasks that are running currently

你离开我真会死。 提交于 2019-12-01 17:24:26

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!

Related Documentation:

getRunningAppProcesses

RunningAppProcessInfo

IMPORTANCE_FOREGROUND

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

If you are not the owner of that task, Android won't give you its info.

I believe getRecentTasks and getRunningTask were deprecated as of API 21. I'm not sure what you're trying to achieve, perhaps u should try getRunningAppProcess. which give u a similar result.

Omer Arif

Use this code Its works for me. Dont forgot to use ListView in your Xml File.

ListView listView = (ListView) findViewById(R.id.listview);
        ActivityManager actvityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
        String[] values = new String[procInfos.size()];
        final String packageName = "my.application.package";
        PackageManager packageManager = getApplicationContext().getPackageManager();
        for (int i = 0; i < procInfos.size(); i++) {
            values[i] = procInfos.get(i).processName;
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
        listView.setAdapter(adapter);  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!