android7.0的ram和rom显示
android7.0的ram和rom分别是运存,机身内存 。
在系统和第三方app显示和获取都会有些不一样,正好最近有在做这方面的研究 。
1. 系统中settings中memory和storage显示分别是由下面决定的:
这两个都可以从设置中代码去查,
storage,查到StorageSettings.java 中private void refresh()和 private void updateSummary() 方法,
final File path = info.getPath();if (path == null) {continue;}privateUsedBytes += path.getTotalSpace() - path.getFreeSpace();privateTotalBytes += path.getTotalSpace();
发现是通过java 方式去获取空间大小的,而7.0的File这个类是在
libcore\ojluni\src\main\java\java\io\File.java定义的
public long getTotalSpace() {SecurityManager sm = System.getSecurityManager();if (sm != null) {sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));sm.checkRead(path);}if (isInvalid()) {return 0L;}return fs.getSpace(this, FileSystem.SPACE_TOTAL); }
public long getFreeSpace() {SecurityManager sm = System.getSecurityManager();if (sm != null) {sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));sm.checkRead(path);}if (isInvalid()) {return 0L;}return fs.getSpace(this, FileSystem.SPACE_FREE);}
RAM:是通过 memory是 ActivityManager.java getMemoryInfo方法,最终outInfo.availMem 又是通过jni调用调用的android_util_Process.cpp 的 android_os_Process_getFreeMemory()
最终android_util_Process.cpp jni调用
static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz)
{static const char* const sums[] = { "MemFree:", "Cached:", NULL };static const size_t sumsLen[] = { strlen("MemFree:"), strlen("Cached:"), 0 };/*jlong tmp = getFreeMemoryImpl(sums, sumsLen, 2); tmp += tmp/2;return tmp;*/return getFreeMemoryImpl(sums, sumsLen, 2)*1.5;//wanlihua *2
}static jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz)
{static const char* const sums[] = { "MemTotal:", NULL };static const size_t sumsLen[] = { strlen("MemTotal:"), 0 };
/* jlong tmp = getFreeMemoryImpl(sums, sumsLen, 1); tmp += tmp/2;return tmp;*/return getFreeMemoryImpl(sums, sumsLen, 1)*1.5 ; //*2;//wanlihua
}
2. 第三方app例如 cpu-z中device中ram和rom的显示是由如下决定的 :
而第三方获取 rom storage大小是通过
StatFs.java
public long getAvailableBytes() {return mStat.f_bavail * mStat.f_bsize;}/*** The total number of bytes supported by the file system.*/public long getTotalBytes() {return mStat.f_blocks * mStat.f_bsize;}
RAM: memory是 ActivityManager.java getMemoryInfo方法,最终outInfo.availMem 又是通过jni调用调用的android_util_Process.cpp 的 android_os_Process_getFreeMemory()
照理totalMemory也可以通过这个方法,
但是cpu-z 反而没用这个方法,用的是直接读取/proc/meminfo 文件中的MemTotal: 1947640 kB 在过滤出数字,并处理。
这个是反编译cpu-z方法得到的,
int i2 = localCPUID.e();int i3 = localCPUID.c(getActivity().getBaseContext());b localb10 = new b("Total RAM", i2 + " MB");this.i.add(localb10);b localb11 = new b("Available RAM", i3 + " MB (" + i3 * 100 / i2 + "%)");localb11.d = 0;public int e(){int i1 = 0;int i2;try{String[] arrayOfString = new java.io.RandomAccessFile("/proc/meminfo", "r").readLine().split(" kB")[0].split(" ");i1 = Integer.parseInt(arrayOfString[(-1 + arrayOfString.length)]);int i3 = Math.round(i1 / 1024);i2 = i3;return i2;}catch (IOException localIOException){i2 = i1;localIOException.printStackTrace();}}
这个proc/meminfo 那么就需要驱动看下节点怎么处理的 。