package com.itheima.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import android.app.ActivityManager; import android.app.ActivityManager.MemoryInfo; import android.content.Context; import android.text.format.Formatter; public class RunRomUtils { //得到运行的程序 public static int getCountRom(Context context){ ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); return am.getRunningAppProcesses().size(); } //得到剩余运行内存空间 public static String getAvail(Context context){ ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); MemoryInfo outInfo = new MemoryInfo(); am.getMemoryInfo(outInfo ); return Formatter.formatFileSize(context, outInfo.availMem); } //得到总运行内存空间 public static String getTotal(Context context){ //4.1.2及以上可以用这个办法 // ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); // MemoryInfo outInfo = new MemoryInfo(); // am.getMemoryInfo(outInfo ); // return Formatter.formatFileSize(context, outInfo.totalMem); File file = new File("/proc/meminfo"); FileInputStream fileIn; try { fileIn = new FileInputStream(file); InputStreamReader in = new InputStreamReader(fileIn); BufferedReader br = new BufferedReader(in); String result = br.readLine(); String total = ""; StringBuffer sb = new StringBuffer(); for (char c : result.toCharArray()) { if(c>=‘0‘&&c<=‘9‘){ sb.append(c); } } int c = Integer.valueOf(sb.toString())*1024; return Formatter.formatFileSize(context,c); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } }
时间: 2024-12-20 03:06:31