任务管理器
实现思路:
ActivityManager类可以获取到当前系统的所有进程,以及每个进程的信息,也可以杀死某个进程,
ActivityManager.getRunningAppProcesses(); 获取系统的所有进程
ActivityManager.getProcessMemoryInfo(pids); 根据进程号获取每个进程的内存占用情况
ActivityManager.restartPackage(processName) ; 根据进程的包名杀死某个进程
PackageManager可以根据包名获取程序的图标、版本号、名字、权限、签名
核心代码:
/* * 获取所有进程及用户 的进程 */ protected void initData() { runningAppProcessInfos =ActivityManager.getRunningAppProcesses(); //获取所有的进程 userRunningAppProcessInfos = new ArrayList<ActivityManager.RunningAppProcessInfo>();//获取用户的进程 for(int i = 0; i< runningAppProcessInfos.size();i++){ if("system".equals(runningAppProcessInfos.get(i).processName) ||"Android.process.media".equals(runningAppProcessInfos.get(i).processName) ||"android.process.acore".equals(runningAppProcessInfos.get(i).processName) ){ continue; } userRunningAppProcessInfos.add(runningAppProcessInfos.get(i)); } } 注:杀死全部进程实际上杀死的是用户的所有进程,系统进程是无法杀死的,所以这里要获取到用户的所有的进程。
/* * 获取某个进程占用的内存信息 */ ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); int[] pids = {userRunningAppProcessInfos.get(position).pid}; Android.os.Debug.MemoryInfo[] memoryInfos = am.getProcessMemoryInfo(pids); int memorysize = memoryInfos[0].getTotalPrivateDirty(); tv_app_memory.setText("内存占用为 "+ memorysize +" KB"); public class TaskInfo { Context context ; PackageManager pm ; public TaskInfo(Context context) { this.context = context; pm = context.getPackageManager(); } /* * 根据包名 查询 图标 */ public Drawable getAppIcon(String packname){ try { ApplicationInfo info = pm.getApplicationInfo(packname, 0); return info.loadIcon(pm); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } /* *获取程序的版本号 */ public String getAppVersion(String packname){ try { PackageInfo packinfo = pm.getPackageInfo(packname, 0); return packinfo.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); return null; } } /* * 获取程序的名字 */ public String getAppName(String packname){ try { ApplicationInfo info = pm.getApplicationInfo(packname, 0); return info.loadLabel(pm).toString(); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } /* * 获取程序的权限 */ public String[] getAppPremission(String packname){ try { PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_PERMISSIONS); //获取到所有的权限 return packinfo.requestedPermissions; } catch (NameNotFoundException e) { e.printStackTrace(); return null; } } /* * 获取程序的签名 */ public String getAppSignature(String packname){ try { PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_SIGNATURES); //获取到所有的权限 return packinfo.signatures[0].toCharsString(); } catch (NameNotFoundException e) { e.printStackTrace(); return null; } } } /* * 杀死所有进程 */ private void killAll() { for (int i = 0 ; i< userRunningAppProcessInfos.size();i++){ System.out.println(userRunningAppProcessInfos.get(i).processName); activitymanager.restartPackage(userRunningAppProcessInfos.get(i).processName); } // 更新显示的界面 pd.show(); new Thread(){ @Override public void run() { initData(); Message msg = new Message(); msg.what = GET_RUNNING_APP_FINISH; hander.sendMessage(msg); } }.start(); } /* * 杀死选择的进程 */ private void killSelected() { for (int i=0 ;i < isselected.size(); i++){ if(isselected.get(i)){ //如果用户也选中则返回true //终止一个activity 需要用到 activitymanager activitymanager.restartPackage(userRunningAppProcessInfos.get(i).processName); } } //更新listview的显示 pd.show(); new Thread(){ @Override public void run() { initData(); Message msg = new Message(); msg.what = GET_RUNNING_APP_FINISH; hander.sendMessage(msg); } }.start(); } /* * 获取系统的剩余内存 */ ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo outInfo = new ActivityManager.MemoryInfo(); am.getMemoryInfo(outInfo); long availMemorySize = outInfo.availMem; String strMemorySize = availMemorySize/1024/1024 + "MB";