Show me the code! – By Davanum Srinivas

December 18, 2007

Android – Task manager primitive prototype

Filed under: Uncategorized — Davanum Srinivas @ 9:27 am

Here’s a screen shot

1

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

    About these ads

10 Comments »

  1. Do you know how to get the PID (ProcessIDs) of the running tasks ?

    Regards

    Comment by plusminus — December 18, 2007 @ 1:19 pm

  2. Opened issue on GoogleGroups:
    http://groups.google.com/group/android-developers/t/8e34f75f21e6ba1f

    Comment by plusminus — December 18, 2007 @ 2:36 pm

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Hi

    Comment by jade — May 22, 2012 @ 9:42 pm

  10. not able to get the zip file

    Comment by Meghal — October 18, 2013 @ 5:23 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 131 other followers

%d bloggers like this: