获取进程列表,需使用ActivityManager中的方法获取进程名;再根据进程名,获取应用程序的ApplicationInfo对象;用ApplicationInfo中的方法为进程加载图标和程序名称。获取服务、任务列表方法同上。
1 实例化ActivityManager
private ActivityManager
mActivityManager;
mActivityManager=(ActivityManager)getSystemService(ACTIVITY_SERVICE);
2 获取各列表
//获取任务列表
public void getTasks()
{
aryListTaskID=new
ArrayList();
aryListTaskName=new
ArrayList();
//以getRunningTasks()取得进程TaskInfo
List
mRunningTasks=
mActivityManager.getRunningTasks(intGetTaskCounter);
for(ActivityManager.RunningTaskInfo
amTask:mRunningTasks)
{
aryListTaskName.add(amTask.baseActivity.getClassName());
aryListTaskID.add(""+amTask.id);
intTaskNum++;
}
tab3_tv1.setText("任务数量:"+intTaskNum);
tab3_tv2.setText("可用内存:"+fileSize(getAvailableRAM()));
}
//获取服务列表
public void getService()
{
linkListServiceIcon=new
LinkedList();
aryListServiceInfo=new
ArrayList();
aryListServiceName=new
ArrayList();
List
serivces =
mActivityManager.getRunningServices(intGetServiceCounter);
for (RunningServiceInfo service:serivces)
{
programUtil.setProcessName(service.process);
// 根据进程名,获取应用程序的ApplicationInfo对象
ApplicationInfo tempAppInfo =
packageUtil.getApplicationInfo(service.process);
// 为进程加载图标和程序名称
if (tempAppInfo != null)
{
linkListServiceIcon.add(tempAppInfo.loadIcon(packageManager));
aryListServiceName.add(tempAppInfo.loadLabel(packageManager).toString());
}
else
{
linkListServiceIcon.add(getApplicationContext().getResources().getDrawable(R.drawable.ic_launcher));
aryListServiceName.add(service.process);
}
//MemInfo是否可得,不行则用包名代替
if(!(mProcessMemoryUtil.getMemInfoByPid(service.pid)==""))
{
aryListServiceInfo.add(mProcessMemoryUtil.getMemInfoByPid(service.pid));
}
else
{
aryListServiceInfo.add(service.process);
}
intServiceNum++;
}
tab2_tv1.setText("服务数量:"+intServiceNum);
tab2_tv2.setText("可用内存:"+fileSize(getAvailableRAM()));
}
//获取进程列表
public void getProcess()
{
linkListProcessIcon=new
LinkedList();
aryListProcessInfo=new
ArrayList();
aryListProcessName=new
ArrayList();
List apps
=
mActivityManager.getRunningAppProcesses();
for(RunningAppProcessInfo app:apps)
{
//包名
// aryListProcessName.add(app.processName);
programUtil.setProcessName(app.processName);
// 根据进程名,获取应用程序的ApplicationInfo对象
ApplicationInfo tempAppInfo =
packageUtil.getApplicationInfo(app.processName);
// 为进程加载图标和程序名称
if (tempAppInfo != null)
{
linkListProcessIcon.add(tempAppInfo.loadIcon(packageManager));
aryListProcessName.add(tempAppInfo.loadLabel(packageManager).toString());
}
else
{
linkListProcessIcon.add(getApplicationContext().getResources().getDrawable(R.drawable.ic_launcher));
aryListProcessName.add(app.processName);
}
//MemInfo是否可得,不行则用包名代替
if(!(mProcessMemoryUtil.getMemInfoByPid(app.pid)==""))
{
aryListProcessInfo.add(mProcessMemoryUtil.getMemInfoByPid(app.pid));
}
else
{
aryListProcessInfo.add(app.processName);
}
intProcessNum++;
}
tab1_tv1.setText("程序数量:"+intProcessNum);
tab1_tv2.setText("可用内存:"+fileSize(getAvailableRAM()));
}
3 PackageUtil.java : 通过一个程序名返回该程序的一个ApplicationInfo对象
package com.me.myexplorer;
import java.util.List;
import android.content.Context;
import
android.content.pm.ApplicationInfo;
import
android.content.pm.PackageManager;
public class PackageUtil
{
//
ApplicationInfo 类,保存了普通应用程序的信息,主要是指Manifest.xml中application标签中的信息
private List
allAppList;
public PackageUtil(Context context) {
// 通过包管理器,检索所有的应用程序(包括卸载)与数据目录
PackageManager pm =
context.getApplicationContext().getPackageManager();
allAppList =
pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
pm.getInstalledPackages(0);
}
public ApplicationInfo getApplicationInfo(String appName)
{
if (appName == null) {
return null;
}
for (ApplicationInfo appinfo : allAppList) {
if (appName.equals(appinfo.processName)) {
return appinfo;
}
}
return null;
}
}