1.本進程不能被選中,所以先將checkbox隱藏掉--手機衛士 不能自殺 2.清理選中進程 3.ProcessInfoProvider.java 1 package com.itheima.mobilesafe74.engine; 2 3 import java.io.BufferedReader ...
1.本進程不能被選中,所以先將checkbox隱藏掉--手機衛士 不能自殺
if(getItem(position).packageName.equals(getPackageName())){ holder.cb_box.setVisibility(View.GONE); }else{ holder.cb_box.setVisibility(View.VISIBLE); }
2.清理選中進程
private void clearAll() { //1,獲取選中進程 //2,創建一個記錄需要殺死的進程的集合 List<ProcessInfo> killProcessList = new ArrayList<ProcessInfo>(); for(ProcessInfo processInfo:mCustomerList){ if(processInfo.getPackageName().equals(getPackageName())){ continue; } if(processInfo.isCheck){ //不能在集合迴圈過程中去移除集合中的對象 // mCustomerList.remove(processInfo); //3,記錄需要殺死的用戶進程 killProcessList.add(processInfo); } } for(ProcessInfo processInfo:mSystemList){ if(processInfo.isCheck){ //4,記錄需要殺死的系統進程 killProcessList.add(processInfo); } } //5,迴圈遍歷killProcessList,然後去移除mCustomerList和mSystemList中的對象 long totalReleaseSpace = 0; for (ProcessInfo processInfo : killProcessList) { //6,判斷當前進程在那個集合中,從所在集合中移除 if(mCustomerList.contains(processInfo)){ mCustomerList.remove(processInfo); } if(mSystemList.contains(processInfo)){ mSystemList.remove(processInfo); } //7,殺死記錄在killProcessList中的進程 ProcessInfoProvider.killProcess(this,processInfo); //記錄釋放空間的總大小 totalReleaseSpace += processInfo.memSize; } //8,在集合改變後,需要通知數據適配器刷新 if(mAdapter!=null){ mAdapter.notifyDataSetChanged(); } //9,進程總數的更新 mProcessCount -= killProcessList.size(); //10,更新可用剩餘空間(釋放空間+原有剩餘空間 == 當前剩餘空間) mAvailSpace += totalReleaseSpace; //11,根據進程總數和剩餘空間大小 tv_process_count.setText("進程總數:"+mProcessCount); tv_memory_info.setText("剩餘/總共"+Formatter.formatFileSize(this, mAvailSpace)+"/"+mStrTotalSpace); //12,通過吐司告知用戶,釋放了多少空間,殺死了幾個進程, String totalRelease = Formatter.formatFileSize(this, totalReleaseSpace); // ToastUtil.show(getApplicationContext(), "殺死了"+killProcessList.size()+"個進程,釋放了"+totalRelease+"空間"); // jni java--c c---java //占位符指定數據%d代表整數占位符,%s代表字元串占位符 ToastUtil.show(getApplicationContext(), String.format("殺死了%d進程,釋放了%s空間", killProcessList.size(),totalRelease)); }
3.ProcessInfoProvider.java
1 package com.itheima.mobilesafe74.engine; 2 3 import java.io.BufferedReader; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.IOException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import com.itheima.mobilesafe74.R; 11 import com.itheima.mobilesafe74.db.domain.ProcessInfo; 12 13 import android.app.ActivityManager; 14 import android.app.ActivityManager.MemoryInfo; 15 import android.app.ActivityManager.RunningAppProcessInfo; 16 import android.content.Context; 17 import android.content.pm.ApplicationInfo; 18 import android.content.pm.PackageManager; 19 import android.content.pm.PackageManager.NameNotFoundException; 20 21 public class ProcessInfoProvider { 22 //獲取進程總數的方法 23 public static int getProcessCount(Context ctx){ 24 //1,獲取activityManager 25 ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); 26 //2,獲取正在運行進程的集合 27 List<RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses(); 28 //3,返回集合的總數 29 return runningAppProcesses.size(); 30 } 31 32 33 /** 34 * @param ctx 35 * @return 返回可用的記憶體數 bytes 36 */ 37 public static long getAvailSpace(Context ctx){ 38 //1,獲取activityManager 39 ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); 40 //2,構建存儲可用記憶體的對象 41 MemoryInfo memoryInfo = new MemoryInfo(); 42 //3,給memoryInfo對象賦(可用記憶體)值 43 am.getMemoryInfo(memoryInfo); 44 //4,獲取memoryInfo中相應可用記憶體大小 45 return memoryInfo.availMem; 46 } 47 48 49 /** 50 * @param ctx 51 * @return 返回總的記憶體數 單位為bytes 返回0說明異常 52 */ 53 public static long getTotalSpace(Context ctx){ 54 /*//1,獲取activityManager 55 ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); 56 //2,構建存儲可用記憶體的對象 57 MemoryInfo memoryInfo = new MemoryInfo(); 58 //3,給memoryInfo對象賦(可用記憶體)值 59 am.getMemoryInfo(memoryInfo); 60 //4,獲取memoryInfo中相應可用記憶體大小 61 return memoryInfo.totalMem;*/ 62 63 //記憶體大小寫入文件中,讀取proc/meminfo文件,讀取第一行,獲取數字字元,轉換成bytes返回 64 FileReader fileReader = null; 65 BufferedReader bufferedReader = null; 66 try { 67 fileReader= new FileReader("proc/meminfo"); 68 bufferedReader = new BufferedReader(fileReader); 69 String lineOne = bufferedReader.readLine(); 70 //將字元串轉換成字元的數組 71 char[] charArray = lineOne.toCharArray(); 72 //迴圈遍歷每一個字元,如果此字元的ASCII碼在0到9的區域內,說明此字元有效 73 StringBuffer stringBuffer = new StringBuffer(); 74 for (char c : charArray) { 75 if(c>='0' && c<='9'){ 76 stringBuffer.append(c); 77 } 78 } 79 return Long.parseLong(stringBuffer.toString())*1024; 80 } catch (Exception e) { 81 e.printStackTrace(); 82 }finally{ 83 try { 84 if(fileReader!=null && bufferedReader!=null){ 85 fileReader.close(); 86 bufferedReader.close(); 87 } 88 } catch (IOException e) { 89 e.printStackTrace(); 90 } 91 } 92 return 0; 93 } 94 95 /** 96 * @param ctx 上下文環境 97 * @return 當前手機正在運行的進程的相關信息 98 */ 99 public static List<ProcessInfo> getProcessInfo(Context ctx){ 100 //獲取進程相關信息 101 List<ProcessInfo> processInfoList = new ArrayList<ProcessInfo>(); 102 //1,activityManager管理者對象和PackageManager管理者對象 103 ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); 104 PackageManager pm = ctx.getPackageManager(); 105 //2,獲取正在運行的進程的集合 106 List<RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses(); 107 108 //3,迴圈遍歷上訴集合,獲取進程相關信息(名稱,包名,圖標,使用記憶體大小,是否為系統進程(狀態機)) 109 for (RunningAppProcessInfo info : runningAppProcesses) { 110 ProcessInfo processInfo = new ProcessInfo(); 111 //4,獲取進程的名稱 == 應用的包名 112 processInfo.packageName = info.processName; 113 //5,獲取進程占用的記憶體大小(傳遞一個進程對應的pid數組) 114 android.os.Debug.MemoryInfo[] processMemoryInfo = am.getProcessMemoryInfo(new int[]{info.pid}); 115 //6,返回數組中索引位置為0的對象,為當前進程的記憶體信息的對象 116 android.os.Debug.MemoryInfo memoryInfo = processMemoryInfo[0]; 117 //7,獲取已使用的大小 118 processInfo.memSize = memoryInfo.getTotalPrivateDirty()*1024; 119 120 try { 121 ApplicationInfo applicationInfo = pm.getApplicationInfo(processInfo.packageName, 0); 122 //8,獲取應用的名稱 123 processInfo.name = applicationInfo.loadLabel(pm).toString(); 124 //9,獲取應用的圖標 125 processInfo.icon = applicationInfo.loadIcon(pm); 126 //10,判斷是否為系統進程 127 if((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM){ 128 processInfo.isSystem = true; 129 }else{ 130 processInfo.isSystem = false; 131 } 132 } catch (NameNotFoundException e) { 133 //需要處理 134 processInfo.name = info.processName; 135 processInfo.icon = ctx.getResources().getDrawable(R.drawable.ic_launcher); 136 processInfo.isSystem = true; 137 e.printStackTrace(); 138 } 139 processInfoList.add(processInfo); 140 } 141 return processInfoList; 142 } 143 144 145 /** 146 * 殺進程方法 147 * @param ctx 上下文環境 148 * @param processInfo 殺死進程所在的javabean的對象 149 */ 150 public static void killProcess(Context ctx,ProcessInfo processInfo) { 151 //1,獲取activityManager 152 ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); 153 //2,殺死指定包名進程(許可權) 154 am.killBackgroundProcesses(processInfo.packageName); 155 } 156 }ProcessInfoProvider