首页 > 代码库 > android系统进程手机内存工具类

android系统进程手机内存工具类

public class TaskUtils {

	/**
	 * 获取正在运行的进程的个数
	 * 
	 * @param context
	 * @return
	 */
	public static int getRunningProcessCount(Context context) {
		// 得到系统的任务管理器.
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		return am.getRunningAppProcesses().size();
	}

	/**
	 * 获取手机的可用内存
	 * 
	 * @param context
	 * @return long类型数据 总的可用内存
	 */
	public static long getAvailRam(Context context) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		ActivityManager.MemoryInfo outInfo = new MemoryInfo();
		am.getMemoryInfo(outInfo);
		return outInfo.availMem;
	}

	/**
	 * 获取手机的总内存
	 * 
	 * @return
	 */
	public static long getTotalRam() {
		try {
			File file = new File("/proc/meminfo");
			FileInputStream fis = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
			// MemTotal: 253604 kB
			String result = br.readLine();
			StringBuffer sb = new StringBuffer();
			char[] chars = result.toCharArray();
			for (char c : chars) {
				if (c >= '0' && c <= '9') {
					sb.append(c);
				}
			}
			return Long.parseLong(sb.toString()) * 1024;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;

		}
	}
}

android系统进程手机内存工具类