views:

459

answers:

2

On android system.I want to get the status of running apps,how to do?

ex. foreground / background!

+1  A: 
Christopher
Then how do all those Task Manager apps exist?
fiXedd
I imagine they use non-public APIs, as in your answer.
Christopher
A: 

A quick Google search provided me this article. The basics are:

In your manifest file add the permission:

<uses-permission id="android.permission.GET_TASKS"/> 

In your Activity do something like this:

 @SuppressWarnings("unchecked")
 private void updateTaskList() {
      /* Grab the Systems IActivityManager. */
      IActivityManager myActivityManager = ActivityManagerNative.getDefault();
      /* Will hold all the task"".toString()"" entries */
      ArrayList<String> listEntries = new ArrayList<String>();
      try {
           int showLimit = 1;
           /* Get all Tasks available (with limit set). */
           List<IActivityManager.TaskInfo> allTasks = myActivityManager
                     .getTasks(showLimit, 0, null);
           int i = 1;
           /* Loop through all tasks returned. */
           for (IActivityManager.TaskInfo aTask : allTasks) {
                listEntries
                          .add("" + (i++) + ": "
                                    + aTask.baseActivity.getClassName() + " ID="
                                    + aTask.id);
           }
      } catch (DeadObjectException e) {
           Log.e("TaskManager", e.getMessage(), e);
      }
      /* Display out listEntries */
      setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1_small, listEntries));
 }

Please note that I haven't tried this... it's just from that site I linked above.

fiXedd
I don't see IActivityManager listed in the docs, so I think this is old code.
Richard
I posted this 9mos ago... things change.
fiXedd