获取Android系统正在运行的线程

时间:2022-02-21 04:57:47
private void getTaskInfos() {
PackageManager manager = getPackageManager();

for (RunningAppProcessInfo app : processes) {
try {
TaskInfo taskInfo = new TaskInfo();
// 获取包名
String processName = app.processName;
taskInfo.setPackagename(processName);

// 获取占用内存
android.os.Debug.MemoryInfo[] memoryInfos = activityManager
.getProcessMemoryInfo(new int[] { app.pid });
long memsize = memoryInfos[0].getTotalPrivateDirty() * 1024;
String memory = Formatter.formatFileSize(this, memsize);
taskInfo.setMemory(memory);

// 获取进程icon name
ApplicationInfo applicationInfo = manager.getApplicationInfo(
processName, 0);
Drawable icon = applicationInfo.loadIcon(manager);
String name = applicationInfo.loadLabel(manager).toString();
taskInfo.setIcon(icon);
taskInfo.setName(name);

if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
// 用户进程
taskInfo.setUserTask(true);
userTaskInfos.add(taskInfo);
} else {
// 系统进程
taskInfo.setUserTask(false);
systemTaskInfos.add(taskInfo);
}

} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(app.processName + "这是异常");
}
}
}