public class ProgressInfoUtil { //獲取進程總數(shù) public static int getProgressCount(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses(); return runningAppProcessInfos.size(); }
/* 返回可用內(nèi)存 */ public static long getAvailSpace(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); activityManager.getMemoryInfo(memoryInfo); return memoryInfo.availMem; }
/* 返回總共內(nèi)存 */ public static long getTotalSpace(Context context) { //totalMem最小版本是16 // ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); // ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); // activityManager.getMemoryInfo(memoryInfo); // return memoryInfo.totalMem; BufferedReader bufferedReader = null; FileReader fileReader = null; try { fileReader = new FileReader("proc/meminfo"); bufferedReader = new BufferedReader(fileReader); String lineOne = bufferedReader.readLine(); char[] charArray = lineOne.toCharArray(); StringBuilder stringBuilder = new StringBuilder(); for (char c : charArray) { if (c >= '0' && c <= '9') stringBuilder.append(c); } return Long.parseLong(stringBuilder.toString()) * 1024; } catch (Exception e) { e.printStackTrace(); } finally { if (fileReader != null && bufferedReader != null) { try { fileReader.close(); bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } return 0; }
/* 返回運行應(yīng)用信息 */ public static ArrayList<ProgressInfoBean> getProgressInfo(Context context) { ArrayList<ProgressInfoBean> progressInfoBeen = new ArrayList<ProgressInfoBean>(); ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); PackageManager packageManager = context.getPackageManager(); List<ActivityManager.RunningAppProcessInfo> list = activityManager.getRunningAppProcesses(); //通過RunningAppProcessInfo和packageManager獲取進程相關(guān)信息,名稱,包名,所占內(nèi)存 for (ActivityManager.RunningAppProcessInfo r : list) { ProgressInfoBean progressInfoBean = new ProgressInfoBean(); //獲取包名 progressInfoBean.packageName = r.processName; //獲取占用內(nèi)存的大小 Debug.MemoryInfo[] memoryInfo = activityManager.getProcessMemoryInfo(new int[]{r.pid}); progressInfoBean.memize = memoryInfo[0].getTotalPrivateDirty() * 1024; try { //獲取應(yīng)用圖標和名稱 ApplicationInfo applicationInfo = packageManager.getApplicationInfo(progressInfoBean.packageName, 0); progressInfoBean.icon = applicationInfo.loadIcon(packageManager); progressInfoBean.name = applicationInfo.loadLabel(packageManager).toString(); if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) { progressInfoBean.isSystem = true; } else { progressInfoBean.isSystem = false; } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); //異常處理 progressInfoBean.icon = context.getResources().getDrawable(R.mipmap.ic_launcher); progressInfoBean.name = r.processName; progressInfoBean.isSystem = true; } progressInfoBeen.add(progressInfoBean); } return progressInfoBeen; } //殺死進程 public static void killProcess(Context context,ProgressInfoBean pb) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); //殺進程要加權(quán)限,kill background process權(quán)限 activityManager.killBackgroundProcesses(pb.packageName); } //殺死所有進程 public static void killAllProcess(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses(); for(ActivityManager.RunningAppProcessInfo r : runningAppProcessInfos) { if(r.processName.equals(context.getPackageName())) { } else { activityManager.killBackgroundProcesses(r.processName); } } } //檢查服務(wù)是否在運行 public static boolean serviceIsRunning(Context ctx, String serviceName){ //1,獲取activityMananger管理者對象,可以去獲取當前手機正在運行的所有服務(wù) ActivityManager mAM = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); //2,獲取手機中正在運行的服務(wù)集合(多少個服務(wù)) List<ActivityManager.RunningServiceInfo> runningServices = mAM.getRunningServices(1000); //3,遍歷獲取的所有的服務(wù)集合,拿到每一個服務(wù)的類的名稱,和傳遞進來的類的名稱作比對,如果一致,說明服務(wù)正在運行 for (ActivityManager.RunningServiceInfo runningServiceInfo : runningServices) { //4,獲取每一個真正運行服務(wù)的名稱 if(serviceName.equals(runningServiceInfo.service.getClassName())){ return true; } } return false; } //檢查程序是否在運行 public static boolean activityIsRunning(Context context,String packageName) { ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100); for (ActivityManager.RunningTaskInfo info : list) { if (info.topActivity.getPackageName().equals(packageName) || info.baseActivity.getPackageName().equals(packageName)) { return true; } } return false; }
|