如何获取 Android 设备的CPU核数,linux 获取时钟频率率以及内存大小

主题信息(必填)
主题描述(最多限制在50个字符)
申请人信息(必填)
申请信息已提交审核,请注意查收邮件,我们会尽快给您反馈。
如有疑问,请联系
傻丫头和高科技产物小心翼翼的初恋
如今的编程是一场程序员和上帝的竞赛,程序员要开发出更大更好、傻瓜都会用到软件。而上帝在努力创造出更大更傻的傻瓜。目前为止,上帝是赢的。个人网站:。个人QQ群:、
个人大数据技术博客:
人生得意须尽欢,莫使金樽空对月。&nbsp&#8250&nbsp&nbsp&#8250&nbsp
如何获取 Android 设备的CPU核数、时钟频率以及内存大小
因项目需要,分析了一下 Facebook 的开源项目 -&。Device Year Class&的主要功能是根据&CPU核数、时钟频率&以及&内存大小&对设备进行分级。代码很简单,只包含两个类:DeviceInfo&-& 获取设备参数,YearClass&-& 根据参数进行分级。下表是 Facebook 公司提供的,其中&Year&栏表示分级结果。YearCoresClockRAM20081528MHz192MB2009n/a600MHz290MB2010n/a1.0GHz512MB201121.2GHz1GB201241.5GHz1.5GB2013n/a2.0GHz2GB2014n/a&2GHz&2GB关于输出年份的计算方法可以参考,本文只把一些比较常用的功能抽取出来做一个简要介绍。获取 CPU 核数我们都知道,Linux 中的设备都是以文件的形式存在,CPU 也不例外,因此 CPU 的文件个数就等价与核数。Android 的 CPU 设备文件位于&/sys/devices/system/cpu/&目录,文件名的的格式为&cpu\d+。root@generic_x86_64:/sys/devices/system/cpu&#&ls
kernel_max
uevent统计一下文件个数便可以获得 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&G&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;
&&&&cores&=&new&File(&/sys/devices/system/cpu/&).listFiles(CPU_FILTER).
&&}&catch&(SecurityException&e)&{
&&&&cores&=&DEVICEINFO_UNKNOWN;
&&}&catch&(NullPointerException&e)&{
&&&&cores&=&DEVICEINFO_UNKNOWN;
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&
&&&&&&return&
&&&&return&
};获取时钟频率获取时钟频率需要读取系统文件 -&/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq&或者&/proc/cpuinfo。我的 Android 模拟器中并没有&cpuinfo_max_freq&文件,因此只能读取&/proc/cpuinfo。/proc/cpuinfo&包含了很多 cpu 数据。processor&&&&:&0
vendor_id&&&&:&GenuineIntel
cpu&family&&&&:&6
model&&&&&&&&:&70
model&name&&&&:&Intel(R)&Core(TM)&i7-4770HQ&CPU&@&2.20GHz
stepping&&&&:&1
cpu&MHz&&&&&&&&:&0.000
cache&size&&&&:&1024&KB
fdiv_bug&&&&:&no
hlt_bug&&&&&&&&:&no
f00f_bug&&&&:&no
coma_bug&&&&:&no
fpu&&&&&&&&:&yes
fpu_exception&&&&:&yes
cpuid&level&&&&:&4
wp&&&&&&&&:&yes代码如下:public&static&int&getCPUMaxFreqKHz()&{
&&int&maxFreq&=&DEVICEINFO_UNKNOWN;
&&&&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&=&freqB
&&&&&&&&}&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&=&freqB
&&&&&&}&finally&{
&&&&&&&&stream.close();
&&}&catch&(IOException&e)&{
&&&&maxFreq&=&DEVICEINFO_UNKNOWN;&//Fall&through&and&return&unknown.
&&return&maxF
}获取内存大小如果 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.totalM
&&&&}&else&{
&&&&&&return&DEVICEINFO_UNKNOWN;
&&}&else&{
&&&&long&totalMem&=&DEVICEINFO_UNKNOWN;
&&&&&&FileInputStream&stream&=&new&FileInputStream(&/proc/meminfo&);
&&&&&&try&{
&&&&&&&&totalMem&=&parseFileForValue(&MemTotal&,&stream);
&&&&&&&&totalMem&*=&1024;
&&&&&&}&finally&{
&&&&&&&&stream.close();
&&&&}&catch&(IOException&e)&{
&&&&return&totalM
上一篇: 现在视频应用越来越火,Periscope火起来后,国内也出现了不少跟风者,界面几乎跟Periscope一模一样.Periscope确实不错,点赞的效果也让人眼前一亮,很漂亮,于是乎,我就想着自己实现一下. 最终的效果图如下: 最终效果图.gif 录制的效果不太好,手机运行起来还是挺好
下一篇: 最近在使用Android Studio+Gradle做一个基础框架SDK项目,该框架主要实现每个app都需要的基础能力,例如网络请求,图片缓存,json解析,日志记录等等。 众所周知,AndroidStudio中应该尽量使用Module来进行模块的划分,既能达到模块解耦的目的,也能在必要

我要回帖

更多关于 linux 获取时钟频率 的文章

 

随机推荐