package org.lxh.demo;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MyActivityRunDemo extends Activity {
private ActivityManager activityManager = null; // ActivityManager对象
private ListAdapter adapter = null; // 适配器组件
private List<String> all = new ArrayList<String>(); // 保存信息
private ListView tasklist = null; // ListView组件
List<ActivityManager.RunningServiceInfo> allServices; // 所有任务信息
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 默认布局管理器
this.tasklist = (ListView) super.findViewById(R.id.tasklist); // 取得组件
this.activityManager = (ActivityManager) super
.getSystemService(Context.ACTIVITY_SERVICE); // 取得运行的服务
this.listActivity();
}
public void listActivity() {
this.allServices = this.activityManager.getRunningServices(30); // 取回30笔任务数量
Iterator<ActivityManager.RunningServiceInfo> iterInfo = allServices
.iterator(); // 实例化Iterator对象
while (iterInfo.hasNext()) { // 迭代输出
ActivityManager.RunningServiceInfo service = iterInfo.next(); // 取出每一个对象
this.all.add("【ID = " + service.pid + " 】 "
+ service.process); // 追加数据
}
this.adapter = new ArrayAdapter<String>(this, // 实例化ArrayAdapter
android.R.layout.simple_list_item_1, // 定义布局文件
MyActivityRunDemo.this.all); // 定义显示数据
this.tasklist.setAdapter(MyActivityRunDemo.this.adapter); // 设置数据
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/tasklist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lxh.demo" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyActivityRunDemo" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
</manifest>