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

Android获取cpu和内存信息、网址的代码 - 推酷
Android获取cpu和内存信息、网址的代码
/** 获取用户硬件信息 */
public static String getMobileInfo() {
//StringBuffer sb = new StringBuffer();
JSONObject mbInfo = new JSONObject();
//通过反射获取用户硬件信息
Field[] fields = Build.class.getDeclaredFields();
for (Field field : fields) {
// 暴力反射,获取私有信息
field.setAccessible(true);
String name = field.getName();
String value = field.get(null).toString();
//sb.append(name + &=& + value);
//sb.append(&\n&);
mbInfo.put(name, value);
} catch (Exception e) {
e.printStackTrace();
//return sb.toString();
return mbInfo.toString();
static public String getCpuString(){
if(Build.CPU_ABI.equalsIgnoreCase(&x86&)){
return &Intel&;
String strInfo = &&;
byte[] bs = new byte[1024];
RandomAccessFile reader = new RandomAccessFile(&/proc/cpuinfo&, &r&);
reader.read(bs);
String ret = new String(bs);
int index = ret.indexOf(0);
if(index != -1) {
strInfo = ret.substring(0, index);
catch (IOException ex){
ex.printStackTrace();
return strI
static public String getCpuType(){
String strInfo = getCpuString();
String strType =
if (strInfo.contains(&ARMv5&)) {
strType = &armv5&;
} else if (strInfo.contains(&ARMv6&)) {
strType = &armv6&;
} else if (strInfo.contains(&ARMv7&)) {
strType = &armv7&;
} else if (strInfo.contains(&Intel&)){
strType = &x86&;
strType = &unknown&;
return strT
if (strInfo.contains(&neon&)) {
strType += &_neon&;
}else if (strInfo.contains(&vfpv3&)) {
strType += &_vfpv3&;
}else if (strInfo.contains(& vfp&)) {
strType += &_vfp&;
strType += &_none&;
return strT
public static CPUInfo getCPUInfo() {
String strInfo =
byte[] bs = new byte[1024];
RandomAccessFile reader = new RandomAccessFile(&/proc/cpuinfo&, &r&);
reader.read(bs);
String ret = new String(bs);
int index = ret.indexOf(0);
if(index != -1) {
strInfo = ret.substring(0, index);
catch (IOException ex)
strInfo = &&;
ex.printStackTrace();
CPUInfo info = parseCPUInfo(strInfo);
info.mCPUMaxFreq = getMaxCpuFreq();
private final static String kCpuInfoMaxFreqFilePath = &/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq&;
private static int getMaxCpuFreq() {
int result = 0;
FileReader fr =
BufferedReader br =
fr = new FileReader(kCpuInfoMaxFreqFilePath);
br = new BufferedReader(fr);
String text = br.readLine();
if (text != null) {
result = Integer.parseInt(text.trim());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null)
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (br != null)
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
public static class CPUInfo{
public CPUInfo(){
public static final int CPU_TYPE_UNKNOWN
public static final int CPU_TYPE_ARMV5TE
public static final int CPU_TYPE_ARMV6
public static final int CPU_TYPE_ARMV7
public static final int CPU_FEATURE_UNKNOWS
public static final int CPU_FEATURE_VFP
public static final int CPU_FEATURE_VFPV3
public static final int CPU_FEATURE_NEON
public int mCPUT
public int mCPUC
public int mCPUF
public double mBogoM
public long mCPUMaxF
* @param cpuInfo
private static CPUInfo parseCPUInfo(String cpuInfo) {
if (cpuInfo == null || &&.equals(cpuInfo)) {
CPUInfo ci = new CPUInfo();
ci.mCPUType = CPUInfo.CPU_TYPE_UNKNOWN;
ci.mCPUFeature = CPUInfo.CPU_FEATURE_UNKNOWS;
ci.mCPUCount = 1;
ci.mBogoMips = 0;
if (cpuInfo.contains(&ARMv5&)) {
ci.mCPUType = CPUInfo.CPU_TYPE_ARMV5TE;
} else if (cpuInfo.contains(&ARMv6&)) {
ci.mCPUType = CPUInfo.CPU_TYPE_ARMV6;
} else if (cpuInfo.contains(&ARMv7&)) {
ci.mCPUType = CPUInfo.CPU_TYPE_ARMV7;
if (cpuInfo.contains(&neon&)) {
ci.mCPUFeature |= CPUInfo.CPU_FEATURE_NEON;
if (cpuInfo.contains(&vfpv3&)) {
ci.mCPUFeature |= CPUInfo.CPU_FEATURE_VFPV3;
if (cpuInfo.contains(& vfp&)) {
ci.mCPUFeature |= CPUInfo.CPU_FEATURE_VFP;
String[] items = cpuInfo.split(&\n&);
for (String item : items) {
if (item.contains(&CPU variant&)) {
int index = item.indexOf(&: &);
if (index &= 0) {
String value = item.substring(index + 2);
ci.mCPUCount = Integer.decode(value);
ci.mCPUCount = ci.mCPUCount == 0 ? 1 : ci.mCPUC
} catch (NumberFormatException e) {
ci.mCPUCount = 1;
} else if (item.contains(&BogoMIPS&)) {
int index = item.indexOf(&: &);
if (index &= 0) {
String value = item.substring(index + 2);
* 获取设备内存大小值
* @return 内存大小,单位MB
public static long getTotalMemory() {
String str1 = &/proc/meminfo&;
String str2;
String[] arrayOfS
long initial_memory = 0;
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
str2 = localBufferedReader.readLine();
if (str2 != null) {
arrayOfString = str2.split(&\\s+&);
initial_memory = Integer.valueOf(arrayOfString[1]).intValue()/1024;
localBufferedReader.close();
return initial_
catch (IOException e)
return -1;
public CPUInfo getCPUInfo() {
String strInfo =
byte[] bs = new byte[1024];
RandomAccessFile reader = new RandomAccessFile(&/proc/cpuinfo&, &r&);
reader.read(bs);
String ret = new String(bs);
int index = ret.indexOf(0);
if(index != -1) {
strInfo = ret.substring(0, index);
catch (IOException ex)
strInfo = &&;
ex.printStackTrace();
CPUInfo info = parseCPUInfo(strInfo);
info.mCPUMaxFreq = getMaxCpuFreq();
* 获取android CPU类型
* @return String CPU类型
public static String getCpuModel(){
String cpu_model = &&;
CPUInfo in = getCPUInfo();
if ((in.mCPUType & CPUInfo.CPU_TYPE_ARMV5TE) == CPUInfo.CPU_TYPE_ARMV5TE)
cpu_model=&armv5&;
else if ((in.mCPUType & CPUInfo.CPU_TYPE_ARMV6) == CPUInfo.CPU_TYPE_ARMV6)
cpu_model=&armv6&;
else if ((in.mCPUType & CPUInfo.CPU_TYPE_ARMV7) == CPUInfo.CPU_TYPE_ARMV7)
cpu_model=&armv7&;
cpu_model=&unknown&;
return cpu_
* 获取android CPU特性
* @return String CPU特性
public static String getCpuFeature(){
String cpu_feature = &&;
CPUInfo in = getCPUInfo();
if ((in.mCPUFeature & CPUInfo.CPU_FEATURE_NEON ) == CPUInfo.CPU_FEATURE_NEON)
cpu_feature=&neon&;
else if ((in.mCPUFeature & CPUInfo.CPU_FEATURE_VFP ) == CPUInfo.CPU_FEATURE_VFP)
cpu_feature=&vfp&;
else if ((in.mCPUFeature & CPUInfo.CPU_FEATURE_VFPV3 ) == CPUInfo.CPU_FEATURE_VFPV3)
cpu_feature=&vfpv3&;
cpu_feature=&unknown&;
return cpu_
* 获取ip地址
* @param mContext
* @return ip地址字符串
public static String getIpAddress(Context mContext) {
String ipAddress =
for (Enumeration&NetworkInterface& en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration&InetAddress& enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
ipAddress = inetAddress.getHostAddress().toString();
} catch (SocketException ex) {
if (DEBUG) {
Log.d(TAG, &ip address:& + ipAddress);
return ipA
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致CPU-Z纯净版|CPU-Z去广告汉化版v1.19 中文安卓版_未来软件园
手机版,更便捷!
当前位置: >
> CPU-Z去广告汉化版v1.19 中文安卓版
CPU-Z去广告汉化版:是一款运行在安卓平台上的手机监控软件,CPU-Z安卓版体积十分小巧,界面简洁完全免费,可以侦测出SoC的型号,架构和每个核心的时钟频率,另外还有系统信息,包括手机品牌和型号,屏幕分辨率,GPU的型号,RAM,ROM,电池信息,传感器等详细信息。CPU-Z安卓版的介绍:CPU-Z是PC领域家喻户晓的CPU检测神器!Android版CPU-Z与桌面PC版本不同,它不仅提供了CPU的信息,还提供了丰富的图形、系统、电池、传感器等信息,可以显示出处理器的型号、主频、内核架构、工艺制程,CPU的负荷情况、屏幕分辨率、RAM内存以及内存负荷情况,让你的手机信息透明化!CPU-Z安卓版的特色:- 查看芯片参数:名称、型号、架构、核心数、修订版本、制程、时钟频率、每个核心的时钟频率、CPU负载、GPU制造商、GPU渲染器;- 查看系统参数:型号、制造商、主板、软件版本、硬件代号、Android版本、内核架构、内核版本、屏幕分辨率、内存大小、内置存储空间、剩余存储空间;- 查看电池参数:健康状态、剩余电量、供电类别、电池状态、电池温度、电池电压;- 查看各种传感器即时参数。CPU-Z功能:# 系统要求:Android 3.0及以上设备# SoC名称、架构、每个核心的时钟频率# 系统信息:设备品牌/型号、屏幕分辨率、内存、存储# 电池信息:电量、状态、温度# 传感器CPU-Z去广告汉化版的说明官方非常厚道,免费提供下载,原版所有功能都不收费。但是为了维持开发,原版会随机在退出的时候显示 Google 提供的广告,有条件的同学可以下载原版点几下广告支持下。我发的版本已经汉化中文,并彻底去除了广告 SDK,大幅缩小了安装包体积,干净不走流量。CPU-Z去广告汉化版的特点:汉化中文、精简语言、去除广告 SDK、对齐!更新日志 CPU-Z for Android V1.19 (03/31/2016)- Samsung Exynos 8890 (M1).- Qualcomm Snapdragon 820 (Kryo).CPU-Z去广告汉化版 V1.17 (11/16/2015)- HiSilicon Kirin 950/940/935 SoCs.- System uptime.- In App Purchase to remove advertisement.
魂斗罗游戏由来已久,在当时与超级马里奥齐名影响了整整一代游戏玩家。魂斗罗是一款射击类游戏,丰富华丽游戏场景、细致的游戏画面、酷炫的游戏技能,让我们体验真正的枪林弹雨。今日小编为大家带来魂斗罗手游合集,游戏继承pc端经典元素与玩法,同时加入真人实时PVP、竞技场的军团社交玩法。魂斗罗手游同样好玩哦,大家快来下载体验吧。
类型:  语言:简体中文
大小:1.31 MB
CPU-Z安卓版: 是一款运行在安卓平台上的手机监控软件,CPU-Z安卓版体积十分小巧,界面简洁完全免费,可以...
类型:  语言:简体中文
大小:6.03 MB
钱多多:一款运行在安卓平台上的金融投资类安卓软件。用户们可以在这里实现非常方便的手机移动端证券交易...
类型:  语言:简体中文
大小:2.95 MB
内圈:一款非常实用的影视圈工作关系交流平台。用户们在这里可以创建一个属于自己的小圈子,什么导演圈...
类型:  语言:简体中文
大小:11.16 MB
周易算命:顾名思义咯,一款算命的软件。人总要有一个对未来的规划和别人的肯定嘛,这些算命,你信它,并...
类型:语言:简体中文
大小:6.5 MBMB
365好老师家长版:一款365好老师推出的教育服务类安卓软件。这款软件是家长版,可以让你与老师实现更加便捷的沟通,方便你了解你的孩子在学...
类型:语言:简体中文
大小:6.05 MBMB
365好老师教师版:一款教育教学类安卓软件。本软件是教师版。作为老师你在这里可以轻松的与孩子的家长进行非常便捷的沟通,也是学校扩展生源...
类型:语言:简体中文
大小:21.3 MBMB
微情:一款社交生活聊天类安卓软件。大家一定不明什么叫社交生活聊天,其实这个就是你可以在这里聊聊生活中那些趣事,聊聊生活经验,交朋友...
类型:语言:简体中文
大小:11.8 MBMB
甜心宝贝:一款手机直播类安卓软件。里面拥有非常多漂亮无比的大美女在线为你直播各种东西,唱歌,跳舞,打游戏,你看想看什么,满足你! 官...
★★★★★
本类推荐排行
版本:v2.2最新安卓版大小:819 KB
版本:v2.3.3最新版大小:11.3MB
版本:v1.9.1手机版大小:11.58MB
版本:v1.2.1最新官方版大小:1.07MB
版本:v1.0.5安卓版大小:8.1 MB
版本:v4.9u3最新官方版大小:3.91MB
版本:v1.2 官方版大小:1.4 MB
版本:v1.5.1安卓版大小:2.3 MB
版本:v3.5.1.46官方最新版大小:13.11MB
版本:V4.4.1 VIP特别版本大小:8.45 MB
版本:v3.1安卓版大小:1.9 MB
版本:安卓版v2.9.9官方版大小:440 KB
版本:最新版安卓版大小:1.6 MB
版本:v9.9.9最新安卓版大小:959 KB
版本:v5.4安卓版大小:7.3 MB如何获取 Android 设备的CPU核数、时钟频率以及内存大小 - 简书
如何获取 Android 设备的CPU核数、时钟频率以及内存大小
因项目需要,分析了一下 Facebook 的开源项目 - 。
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 G that can let an app in the background run without impacting
// the foreground application. But for our purposes, it makes them single core.
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() {
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') {
获取时钟频率
获取时钟频率需要读取系统文件 - /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq 或者 /proc/cpuinfo。
我的 Android 模拟器中并没有 cpuinfo_max_freq 文件,因此只能读取 /proc/cpuinfo。
/proc/cpuinfo 包含了很多 cpu 数据。
: 0vendor_id
: GenuineIntelcpu family
: 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
: yesfpu_exception
: yescpuid level
代码如下:
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);
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");
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
return DEVICEINFO_UNKNOWN;
long totalMem = DEVICEINFO_UNKNOWN;
FileInputStream stream = new FileInputStream("/proc/meminfo");
totalMem = parseFileForValue("MemTotal", stream);
totalMem *= 1024;
} finally {
stream.close();
} catch (IOException e) {
return totalM

我要回帖

更多关于 cpu时钟频率 的文章

 

随机推荐