73获取正在运行的进程的信息

时间:2022-04-13 04:58:41

获取正在运行的进程信息:包括进程的名字,进程和进程之间的区别(包名),图标,占用内存的大小,用户进程还是系统进程:

显示实体类:

package com.ustc.mobilemanager.domain;

import android.graphics.drawable.Drawable;

/**
* 进程信息的业务bean
*
* @author
*
*/
public class TaskInfo {

private Drawable icon;
private String name;
private String packname;
private long memSize;// 内存占用大小
private boolean userTask;// 用户进程(true)or系统进程(false)

public Drawable getIcon() {
return icon;
}

public void setIcon(Drawable icon) {
this.icon = icon;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPackname() {
return packname;
}

public void setPackname(String packname) {
this.packname = packname;
}

public long getMemSize() {
return memSize;
}

public void setMemSize(long memSize) {
this.memSize = memSize;
}

public boolean isUserTask() {
return userTask;
}

public void setUserTask(boolean userTask) {
this.userTask = userTask;
}

@Override
public String toString() {
return "TaskInfo [icon=" + icon + ", name=" + name + ", packname="
+ packname + ", memSize=" + memSize + ", userTask=" + userTask
+ "]";
}

}
业务类:

package com.ustc.mobilemanager.engine;

import java.util.ArrayList;
import java.util.List;

import com.ustc.mobilemanager.R;
import com.ustc.mobilemanager.domain.TaskInfo;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Debug.MemoryInfo;

/**
*
* 提供手机里面的进程信息
*
* @author
*
*/
public class TaskInfoProvider {

/**
* 获取所有的进程信息
*
* @param context
* @return
*/
public static List<TaskInfo> getTaskInfo(Context context) {

ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
PackageManager pm = context.getPackageManager();
List<RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();

List<TaskInfo> taskInfos = new ArrayList<TaskInfo>();
for (RunningAppProcessInfo processInfo : processInfos) {

TaskInfo taskInfo = new TaskInfo();

// 进程名就是包名
String packname = processInfo.processName;

taskInfo.setPackname(packname);

// import android.os.Debug.MemoryInfo导包不能导错了
MemoryInfo[] memoryInfos = am
.getProcessMemoryInfo(new int[] { processInfo.pid });
// 转成byte,方便转化
long memsize = memoryInfos[0].getTotalPrivateDirty() * 1024l;
taskInfo.setMemSize(memsize);

try {
// PackageInfo packageInfo = pm.getPackageInfo(packname, 0);
// packageInfo.applicationInfo

ApplicationInfo applicationInfo = pm.getApplicationInfo(
packname, 0);
Drawable icon = applicationInfo.loadIcon(pm);
taskInfo.setIcon(icon);

String name = applicationInfo.loadLabel(pm).toString();

taskInfo.setName(name);

if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
// 用户进程
taskInfo.setUserTask(true);

} else {
// 系统进程

taskInfo.setUserTask(false);
}

} catch (Exception e) {
e.printStackTrace();
//系统内核进程 没有名称
taskInfo.setName(packname);
taskInfo.setIcon(context.getResources().getDrawable(R.drawable.icon));
}
taskInfos.add(taskInfo);
}
return taskInfos;
}

}

方法也是通过ActivityManager的getRunningAppProcesses获得RunningAppProcessInfo的List集合,对这个集合做遍历:

RunningAppProcessInfo的processName就是包名;将进程的pid传入ActivityManager的getProcessMemoryInfo方法,得到MemoryInfo数组,通过getTotalPrivateDirty方法,得到进程占用手机内存的大小;

package com.ustc.mobilemanager.engine;

import java.util.ArrayList;
import java.util.List;

import com.ustc.mobilemanager.R;
import com.ustc.mobilemanager.domain.TaskInfo;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Debug.MemoryInfo;

/**
*
* 提供手机里面的进程信息
*
* @author
*
*/
public class TaskInfoProvider {

/**
* 获取所有的进程信息
*
* @param context
* @return
*/
public static List<TaskInfo> getTaskInfo(Context context) {

ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
PackageManager pm = context.getPackageManager();
List<RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();

List<TaskInfo> taskInfos = new ArrayList<TaskInfo>();
for (RunningAppProcessInfo processInfo : processInfos) {

TaskInfo taskInfo = new TaskInfo();

// 进程名就是包名
String packname = processInfo.processName;

taskInfo.setPackname(packname);

// import android.os.Debug.MemoryInfo导包不能导错了
MemoryInfo[] memoryInfos = am
.getProcessMemoryInfo(new int[] { processInfo.pid });
// 转成byte,方便转化
long memsize = memoryInfos[0].getTotalPrivateDirty() * 1024l;
taskInfo.setMemSize(memsize);

try {
// PackageInfo packageInfo = pm.getPackageInfo(packname, 0);
// packageInfo.applicationInfo

ApplicationInfo applicationInfo = pm.getApplicationInfo(
packname, 0);
Drawable icon = applicationInfo.loadIcon(pm);
taskInfo.setIcon(icon);

String name = applicationInfo.loadLabel(pm).toString();

taskInfo.setName(name);

if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
// 用户进程
taskInfo.setUserTask(true);

} else {
// 系统进程

taskInfo.setUserTask(false);
}

} catch (Exception e) {
e.printStackTrace();
//系统内核进程 没有名称
taskInfo.setName(packname);
taskInfo.setIcon(context.getResources().getDrawable(R.drawable.icon));
}
taskInfos.add(taskInfo);
}
return taskInfos;
}

}

进程名和图标在applicationInfo这个节点下,判断是否是用户程序,只需要和ApplicationInfo.FLAG_SYSTEM做与运算就可以了,测试代码如下:

package com.ustc.mobilemanager.test;

import java.util.List;

import com.ustc.mobilemanager.domain.TaskInfo;
import com.ustc.mobilemanager.engine.TaskInfoProvider;

import android.test.AndroidTestCase;

public class TestTaskInfoProvider extends AndroidTestCase {

public void testGetTaskInfo() throws Exception {

List<TaskInfo> infos = TaskInfoProvider.getTaskInfo(getContext());

for (TaskInfo taskInfo : infos) {
System.out.println(taskInfo.toString());
}

}

}


打印:

73获取正在运行的进程的信息



不过,也引发了一些错误:(提示NameNotFoundException)

73获取正在运行的进程的信息


这个System的error未解决。要是知道怎么解决的朋友指导下。(我想应该不是代码的问题)。