如何获取 Android 设备的CPU核数、时钟频率以及内存大小
来源:互联网 发布:2016最新网络诈骗方法 编辑:程序博客网 时间:2024/04/30 03:10
因项目需要,分析了一下 Facebook 的开源项目 - Device Year Class。
Device Year Class 的主要功能是根据 CPU核数、时钟频率 以及 内存大小 对设备进行分级。代码很简单,只包含两个类:
DeviceInfo
-> 获取设备参数,YearClass
-> 根据参数进行分级。
下表是 Facebook 公司提供的分级标准,其中 Year
栏表示分级结果。
关于输出年份的计算方法可以参考源码,本文只把一些比较常用的功能抽取出来做一个简要介绍。
获取 CPU 核数
我们都知道,Linux 中的设备都是以文件的形式存在,CPU 也不例外,因此 CPU 的文件个数就等价与核数。
Android 的 CPU 设备文件位于 /sys/devices/system/cpu/
目录,文件名的的格式为 cpu\d+
。
root@generic_x86_64:/sys/devices/system/cpu # lscpu0cpufreqcpuidlekernel_maxmodaliasofflineonlinepossiblepowerpresentuevent
统计一下文件个数便可以获得 CPU 核数。
public static int getNumberOfCPUCores() { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) { // Gingerbread doesn't support giving a single application access to both cores, but a // handful of devices (Atrix 4G and Droid X2 for example) were released with a dual-core // chipset and Gingerbread; that can let an app in the background run without impacting // the foreground application. But for our purposes, it makes them single core. return 1; } int cores; try { cores = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length; } catch (SecurityException e) { cores = DEVICEINFO_UNKNOWN; } catch (NullPointerException e) { cores = DEVICEINFO_UNKNOWN; } return cores;}private static final FileFilter CPU_FILTER = new FileFilter() { @Override public boolean accept(File pathname) { String path = pathname.getName(); //regex is slow, so checking char by char. if (path.startsWith("cpu")) { for (int i = 3; i < path.length(); i++) { if (path.charAt(i) < '0' || path.charAt(i) > '9') { return false; } } return true; } return false; }};
获取时钟频率
获取时钟频率需要读取系统文件 - /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
或者 /proc/cpuinfo
。
我的 Android 模拟器中并没有 cpuinfo_max_freq
文件,因此只能读取 /proc/cpuinfo
。
/proc/cpuinfo
包含了很多 cpu 数据。
processor : 0vendor_id : GenuineIntelcpu family : 6model : 70model name : Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHzstepping : 1cpu MHz : 0.000cache size : 1024 KBfdiv_bug : nohlt_bug : nof00f_bug : nocoma_bug : nofpu : yesfpu_exception : yescpuid level : 4wp : yes
代码如下:
public static int getCPUMaxFreqKHz() { int maxFreq = DEVICEINFO_UNKNOWN; try { for (int i = 0; i < getNumberOfCPUCores(); i++) { String filename = "/sys/devices/system/cpu/cpu" + i + "/cpufreq/cpuinfo_max_freq"; File cpuInfoMaxFreqFile = new File(filename); if (cpuInfoMaxFreqFile.exists()) { byte[] buffer = new byte[128]; FileInputStream stream = new FileInputStream(cpuInfoMaxFreqFile); try { stream.read(buffer); int endIndex = 0; //Trim the first number out of the byte buffer. while (buffer[endIndex] >= '0' && buffer[endIndex] <= '9' && endIndex < buffer.length) endIndex++; String str = new String(buffer, 0, endIndex); Integer freqBound = Integer.parseInt(str); if (freqBound > maxFreq) maxFreq = freqBound; } catch (NumberFormatException e) { //Fall through and use /proc/cpuinfo. } finally { stream.close(); } } } if (maxFreq == DEVICEINFO_UNKNOWN) { FileInputStream stream = new FileInputStream("/proc/cpuinfo"); try { int freqBound = parseFileForValue("cpu MHz", stream); freqBound *= 1000; //MHz -> kHz if (freqBound > maxFreq) maxFreq = freqBound; } finally { stream.close(); } } } catch (IOException e) { maxFreq = DEVICEINFO_UNKNOWN; //Fall through and return unknown. } return maxFreq;}
获取内存大小
如果 SDK 版本大于等于 JELLY_BEAN
,可以通过 ActivityManager
来获取内从大小。
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);am.getMemoryInfo(memInfo);
如果版本低于 JELLY_BEAN
,则只能读取系统文件了。
FileInputStream stream = new FileInputStream("/proc/meminfo");totalMem = parseFileForValue("MemTotal", stream);
完整代码如下:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)public static long getTotalMemory(Context c) { // memInfo.totalMem not supported in pre-Jelly Bean APIs. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo(); ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE); am.getMemoryInfo(memInfo); if (memInfo != null) { return memInfo.totalMem; } else { return DEVICEINFO_UNKNOWN; } } else { long totalMem = DEVICEINFO_UNKNOWN; try { FileInputStream stream = new FileInputStream("/proc/meminfo"); try { totalMem = parseFileForValue("MemTotal", stream); totalMem *= 1024; } finally { stream.close(); } } catch (IOException e) { } return totalMem; }}
猜你喜欢
- 完全掌握 Android Data Binding
- Android 流式布局 - FlowLayout
- ViewPager 实现的图片 Slider
2 0
- 如何获取 Android 设备的CPU核数、时钟频率以及内存大小
- 如何获取 Android 设备的CPU核数、时钟频率以及内存大小
- 如何获取 Android 设备的CPU核数、时钟频率以及内存大小
- 如何获取 Android 设备的CPU核数、时钟频率以及内存大小
- 如何获取 Android 设备的CPU核数、时钟频率以及内存大小
- Android获取设备CPU核数以及内存大小的方法
- 获取Android设备的CPU频率
- CPU的时钟频率
- android 的cpu 频率 CPU核心数
- android 如何获取CPU频率(HZ)
- 获取手机设备的CPU核数
- 获取MIC的核数、内存大小
- android 如何获取CPU频率(HZ…
- 内核检测CPU的时钟频率
- 在MTK芯片上如何控制CPU的核数和频率-MTK PerfService
- 获取cpu频率的代码
- Android 如何获取App内存大小
- 实时获取Android手机CPU频率
- lightoj - 1205 Palindromic Numbers (数位dp)好
- STL中常用的vector,map,set,sort 用法
- 紫阳的日常——第一章 拒绝访问的高考成绩
- Redis与Memcached的区别
- [傻瓜式教程]使用adb root电视盒子
- 如何获取 Android 设备的CPU核数、时钟频率以及内存大小
- 我的Java开发学习之旅------>Java经典排序算法之希尔排序
- Linked List Cycle
- Ruby中的语句中断和返回
- 自定义JSTL标签
- org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist:
- WIN7x64+VS2013+OpenCV2.4.11+CUDA7.0配置操作说明书(题目真无聊....)
- 一个LINUX狂人的语录
- JSP技术基础