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));
}
}
No comments:
Post a Comment