自己写的一些工具类
public class TaskInfoUtils { /** * 获取全部进程的信息 * * @param pContext * @return */ public static List<TaskInfo> getTaskInfos(Context pContext) { // 1获取到ActivityManager和PackageManager ActivityManager _ActivityManager = (ActivityManager) pContext .getSystemService(pContext.ACTIVITY_SERVICE); PackageManager _PackageManager = pContext.getPackageManager(); List<TaskInfo> _TaskInfos = new ArrayList<TaskInfo>(); // 2获取到所有在运行的进程 List<RunningAppProcessInfo> _RunningAppProcessInfos = _ActivityManager .getRunningAppProcesses(); //3 遍历进程,获取他的信息 for (RunningAppProcessInfo runningAppProcessInfo : _RunningAppProcessInfos) { TaskInfo _TaskInfo = new TaskInfo(); MemoryInfo[] memoryInfos = _ActivityManager .getProcessMemoryInfo(new int[] { runningAppProcessInfo.pid }); long _MemSize = memoryInfos[0].getTotalPrivateDirty() * 1024;// 进程消耗的内存 String _packageName = runningAppProcessInfo.processName;// 进程的包 ApplicationInfo _ApplicationInfo; try { _ApplicationInfo = _PackageManager.getApplicationInfo( _packageName, 0); Drawable _Icon = _ApplicationInfo.loadIcon(_PackageManager);// 程序图标 String _Name = _ApplicationInfo.loadLabel(_PackageManager) .toString();// 程序名 _TaskInfo.setIcon(_Icon); _TaskInfo.setMemsize(_MemSize); _TaskInfo.setName(_Name); _TaskInfo.setPackname(_packageName); if ((_ApplicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { // 用户进程 _TaskInfo.setUserTask(true); } else { // 系统进程 _TaskInfo.setUserTask(false); } _TaskInfos.add(_TaskInfo); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return _TaskInfos; } class TaskInfo { private Drawable icon; private String name; private long memsize; private boolean userTask; private String packname; 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 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; } public String getPackname() { return packname; } public void setPackname(String packname) { this.packname = packname; } @Override public String toString() { return "TaskInfo [ name=" + name + ", memsize=" + memsize + ", userTask=" + userTask + ", packname=" + packname + "]"; } } }