android开发获取手机内存使用情况

public Object getPenter() {
    String dir = "/proc/meminfo";
    try {
        FileReader fr = new FileReader(dir);
        BufferedReader br = new BufferedReader(fr, 2048);
        String memoryLine = br.readLine();
        String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
        br.close();
        long totalMemorySize = Integer.parseInt(subMemoryLine.replaceAll("\\D+", ""));
        long availableSize = getAvailableMemory(MainActivity.this) / 1024;
        int percent = (int) ((totalMemorySize - availableSize) / (float) totalMemorySize * 100);
        return percent + "%";
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "无结果";
}
private  long getAvailableMemory(Context context) {
    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    getActivityManager(context).getMemoryInfo(mi);
    return mi.availMem;
}
public  ActivityManager getActivityManager(Context context) {
    if (mActivityManager == null) {
        mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    }
    return mActivityManager;
}

你可能感兴趣的:(android开发获取手机内存使用情况)