0

I'm trying to load all running tasks to a list view in Android, I done it for all installed applications, but I don't know how to get it for running tasks.

I' could display the running tasks on toast message, but don't know how to get that on ListView. Please help me.

Able Alias
  • 3,824
  • 11
  • 58
  • 87
  • yes, this answer showing how to get running tasks package name, but I need to know how to load that in a ListView layout – Able Alias Jan 02 '14 at 22:55

1 Answers1

2

As mentioned in the comments, you'll first want to get a list of the currently running tasks. You can do so with the following code:

final   ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

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

Source: How to find the currently running applications programatically in android?

Then once you have the list of current tasks, you'll want to create an Adapter, which will populate your list view. There are two main ways you could do this:

  • Use a standard ArrayAdapter and pass in a list of Strings. You would have to generate this list of Strings from the list of tasks that were returned to you. This is a great way to display only a single line of text for each task per list element.
  • Extend the standard ArrayAdapter to accept a list of Tasks (RunningTaskInfo) and resolve the content inside the ArrayAdapter subclass. This is necessary if you want to display more than a single line of text for each task per list element, for example, the task ID or an image.

Both examples are explained well in the following tutorial: http://www.vogella.com/articles/AndroidListView/article.html#listview_listviewexample

Finally, you will want to associate your Adapter to your ListView by calling:

listViewReference.setAdapter(yourAdapterReference);

Your ListView will then be populated automatically by your adapter.

Community
  • 1
  • 1
TheIT
  • 11,919
  • 4
  • 64
  • 56