Android - Task manager primitive prototype

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 this entry