6

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?

Any help would be greatly appreciated.

Thanks.

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+"");
        }
    }
Viking93
  • 269
  • 2
  • 3
  • 15

4 Answers4

4

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 :)

Shane Duffy
  • 1,117
  • 8
  • 18
1

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

Hassan Naqvi
  • 424
  • 6
  • 18
  • But if you see voodoo app, they show their widget only in certain apps.So i thought this might be possible.But if there is any work around for such a problem please suggest me the way. – Viking93 Mar 05 '16 at 10:24
0

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);  
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
Omer Arif
  • 31
  • 1
-1

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.