Here’s a screen shot

Setup the Android Manifest with the “android.permission.GET_TASKS” permission
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.apache.hello">
<uses-permission id="android.permission.GET_TASKS"/>
<application>
<activity class=".HelloApp" android:label="HelloApp">
<intent-filter>
<action android:value="android.intent.action.MAIN" />
<category android:value="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Code a simple ListActivity
package org.apache.hello;
import android.app.ActivityManagerNative;
import android.app.IActivityManager;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class HelloApp extends ListActivity {
final IActivityManager manager = ActivityManagerNative.getDefault();
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
updateTaskList();
this.getListView().setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView adapterview, View view, int i, long l) {
try {
manager.moveTaskToFront(i);
} catch (DeadObjectException e) {
Log.e("HelloApp", e.getMessage(), e);
}
}
public void onNothingSelected(AdapterView adapterview) {
}
});
}
public void onWindowFocusChanged(boolean flag) {
updateTaskList();
}
private void updateTaskList() {
ArrayList items = new ArrayList();
try {
List tasks = manager.getTasks(10, 0, null);
int i = 1;
for (Iterator iterator = tasks.iterator(); iterator.hasNext();) {
IActivityManager.TaskInfo item = (IActivityManager.TaskInfo) iterator.next();
items.add(new String((i++) + " : " + item.baseActivity.getPackageName()));
}
} catch (DeadObjectException e) {
Log.e("HelloApp", e.getMessage(), e);
}
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, items));
}
}
Running the sample
- Install the APK using “adb install HelloApp.apk” as usual
- Click to open the app and click on any listed application to switch to it
Download Source and APK from here – TaskList.zip
Do you know how to get the PID (ProcessIDs) of the running tasks ?
Regards
Comment by plusminus — December 18, 2007 @ 1:19 pm
Opened issue on GoogleGroups:
http://groups.google.com/group/android-developers/t/8e34f75f21e6ba1f
Comment by plusminus — December 18, 2007 @ 2:36 pm
hi in this above code
what do u mean by
IActivityManager and ActivityManagerNative
i am using 1.6 sdk apis
so can u guide me from whr i can get this two class
Comment by Umakant — December 3, 2009 @ 6:59 am
Where I can to download the source code, the TaskList.zip file??
Thanks a lot!!
LARA
Comment by lara — April 4, 2010 @ 5:37 am
I have a lot of problems with the next line:
import android.app.IActivityManager;
I have the 1.6 SDK !!
Could you help me please?????????
Thank you. LARA
Comment by lara — April 4, 2010 @ 6:22 am
Hi I am also aving same issues as LARA above about IActivityManager. Can you throw some light on this
Vikrant
Comment by Vikrant — July 4, 2010 @ 9:50 am
Hi, Thanks for posting.
I would like to know How can i know the current running activity ?
Comment by JettiMadhuChowdary — December 9, 2011 @ 3:15 am
This code seems to be based on Android version m3-rc37a. You can see the IActivityManager javadocs here: http://www.androidjavadoc.com/m3-rc37a/android/app/IActivityManager.html
m3-rc37a was a pre-release version of Android so this interface was probably removed (or hidden) from published API before releasing Android 1.0.
tl;dr – the interface is inaccessible in Android – this code won’t work.
Comment by TGC — March 5, 2012 @ 3:07 pm
Hi
Comment by jade — May 22, 2012 @ 9:42 pm
not able to get the zip file
Comment by Meghal — October 18, 2013 @ 5:23 am