Android获取设备CPU核数、时钟频率以及内存大小的方法
本文实例讲述了Android获取设备CPU核数、时钟频率以及内存大小的方法。分享给大家供大家参考,具体如下:
因项目需要,分析了一下Facebook的开源项目-DeviceYearClass。
DeviceYearClass的主要功能是根据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#ls cpu0 cpufreq cpuidle kernel_max modalias offline online possible power present uevent
统计一下文件个数便可以获得CPU核数。
publicstaticintgetNumberOfCPUCores(){ if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.GINGERBREAD_MR1){ //Gingerbreaddoesn'tsupportgivingasingleapplicationaccesstobothcores,buta //handfulofdevices(Atrix4GandDroidX2forexample)werereleasedwithadual-core //chipsetandGingerbread;thatcanletanappinthebackgroundrunwithoutimpacting //theforegroundapplication.Butforourpurposes,itmakesthemsinglecore. return1; } intcores; try{ cores=newFile("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length; }catch(SecurityExceptione){ cores=DEVICEINFO_UNKNOWN; }catch(NullPointerExceptione){ cores=DEVICEINFO_UNKNOWN; } returncores; } privatestaticfinalFileFilterCPU_FILTER=newFileFilter(){ @Override publicbooleanaccept(Filepathname){ Stringpath=pathname.getName(); //regexisslow,socheckingcharbychar. if(path.startsWith("cpu")){ for(inti=3;i<path.length();i++){ if(path.charAt(i)<'0'||path.charAt(i)>'9'){ returnfalse; } } returntrue; } returnfalse; } };
获取时钟频率
获取时钟频率需要读取系统文件-/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
cpufamily :6
model :70
modelname :Intel(R)Core(TM)i7-4770HQCPU@2.20GHz
stepping :1
cpuMHz :0.000
cachesize :1024KB
fdiv_bug :no
hlt_bug :no
f00f_bug :no
coma_bug :no
fpu :yes
fpu_exception :yes
cpuidlevel:4
wp :yes
代码如下:
publicstaticintgetCPUMaxFreqKHz(){ intmaxFreq=DEVICEINFO_UNKNOWN; try{ for(inti=0;i<getNumberOfCPUCores();i++){ Stringfilename= "/sys/devices/system/cpu/cpu"+i+"/cpufreq/cpuinfo_max_freq"; FilecpuInfoMaxFreqFile=newFile(filename); if(cpuInfoMaxFreqFile.exists()){ byte[]buffer=newbyte[128]; FileInputStreamstream=newFileInputStream(cpuInfoMaxFreqFile); try{ stream.read(buffer); intendIndex=0; //Trimthefirstnumberoutofthebytebuffer. while(buffer[endIndex]>='0'&&buffer[endIndex]<='9' &&endIndex<buffer.length)endIndex++; Stringstr=newString(buffer,0,endIndex); IntegerfreqBound=Integer.parseInt(str); if(freqBound>maxFreq)maxFreq=freqBound; }catch(NumberFormatExceptione){ //Fallthroughanduse/proc/cpuinfo. }finally{ stream.close(); } } } if(maxFreq==DEVICEINFO_UNKNOWN){ FileInputStreamstream=newFileInputStream("/proc/cpuinfo"); try{ intfreqBound=parseFileForValue("cpuMHz",stream); freqBound*=1000;//MHz->kHz if(freqBound>maxFreq)maxFreq=freqBound; }finally{ stream.close(); } } }catch(IOExceptione){ maxFreq=DEVICEINFO_UNKNOWN;//Fallthroughandreturnunknown. } returnmaxFreq; }
获取内存大小
如果SDK版本大于等于JELLY_BEAN,可以通过ActivityManager来获取内从大小。
ActivityManager.MemoryInfomemInfo=newActivityManager.MemoryInfo(); ActivityManageram=(ActivityManager)c.getSystemService(Context.ACTIVITY_SERVICE); am.getMemoryInfo(memInfo);
如果版本低于JELLY_BEAN,则只能读取系统文件了。
FileInputStreamstream=newFileInputStream("/proc/meminfo"); totalMem=parseFileForValue("MemTotal",stream);
完整代码如下:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) publicstaticlonggetTotalMemory(Contextc){ //memInfo.totalMemnotsupportedinpre-JellyBeanAPIs. if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){ ActivityManager.MemoryInfomemInfo=newActivityManager.MemoryInfo(); ActivityManageram=(ActivityManager)c.getSystemService(Context.ACTIVITY_SERVICE); am.getMemoryInfo(memInfo); if(memInfo!=null){ returnmemInfo.totalMem; }else{ returnDEVICEINFO_UNKNOWN; } }else{ longtotalMem=DEVICEINFO_UNKNOWN; try{ FileInputStreamstream=newFileInputStream("/proc/meminfo"); try{ totalMem=parseFileForValue("MemTotal",stream); totalMem*=1024; }finally{ stream.close(); } }catch(IOExceptione){ } returntotalMem; } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android操作XML数据技巧总结》、《Android编程之activity操作技巧总结》、《Android资源操作技巧汇总》、《Android文件操作技巧汇总》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。