任務管理器可以實現的功能有:
1.查看當前系統下運行的所有的進程
2.可以查看每個進程的進程號、版本號以及內存占用情況
3.殺死進程(可以殺死全部進程或者殺死指定的進程)
4.查看系統剩余內存
效果圖: 殺死全部進程
實現思路:
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;
- }
- }
- }