Android系统信息获取 之三:CPU信息获取
2016-07-14 13:34
417 查看
(转载)http://blog.csdn.net/netwalk/article/details/9793501
Android系统的CPU信息涉及到两个文件:/proc/cpuinfo和/proc/stat
通过读取文件/proc/cpuinfo,来获取系统CPU的类型等多种信息,
通过读取/proc/stat 所有CPU活动的信息来计算CPU使用率。
获取CPU信息可参考下面代码
1、
[java] view
plain copy
// 获取CPU名字
public static String getCpuName() {
try {
FileReader fr = new FileReader("/proc/cpuinfo");
BufferedReader br = new BufferedReader(fr);
String text = br.readLine();
String[] array = text.split(":\\s+", 2);
for (int i = 0; i < array.length; i++) {
}
return array[1];
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
plain copy
// 获取CPU最大频率
// "/system/bin/cat" 命令行
// "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" 存储最大频率的文件的路径
public static String getMaxCpuFreq() {
String result = "";
ProcessBuilder cmd;
try {
String[] args = { "/system/bin/cat",
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
cmd = new ProcessBuilder(args);
Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[24];
while (in.read(re) != -1) {
result = result + new String(re);
}
in.close();
} catch (IOException ex) {
ex.printStackTrace();
result = "N/A";
}
return result.trim();
}
plain copy
// 获取CPU最小频率
public static String getMinCpuFreq() {
String result = "";
ProcessBuilder cmd;
try {
String[] args = { "/system/bin/cat",
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };
cmd = new ProcessBuilder(args);
Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[24];
while (in.read(re) != -1) {
result = result + new String(re);
}
in.close();
} catch (IOException ex) {
ex.printStackTrace();
result = "N/A";
}
return result.trim();
}
plain copy
// 实时获取CPU当前频率
public static String getCurCpuFreq() {
String result = "N/A";
try {
FileReader fr = new FileReader(
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
BufferedReader br = new BufferedReader(fr);
String text = br.readLine();
result = text.trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//-----------------------------------------------------------------------------------------------------------------
直接使用命令来查看CPU的型号:
Android系统的CPU信息涉及到两个文件:/proc/cpuinfo和/proc/stat
通过读取文件/proc/cpuinfo,来获取系统CPU的类型等多种信息,
通过读取/proc/stat 所有CPU活动的信息来计算CPU使用率。
获取CPU信息可参考下面代码
1、
获取CPU名字
[java] viewplain copy
// 获取CPU名字
public static String getCpuName() {
try {
FileReader fr = new FileReader("/proc/cpuinfo");
BufferedReader br = new BufferedReader(fr);
String text = br.readLine();
String[] array = text.split(":\\s+", 2);
for (int i = 0; i < array.length; i++) {
}
return array[1];
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
2、获取CPU最大频率
[java] viewplain copy
// 获取CPU最大频率
// "/system/bin/cat" 命令行
// "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" 存储最大频率的文件的路径
public static String getMaxCpuFreq() {
String result = "";
ProcessBuilder cmd;
try {
String[] args = { "/system/bin/cat",
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
cmd = new ProcessBuilder(args);
Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[24];
while (in.read(re) != -1) {
result = result + new String(re);
}
in.close();
} catch (IOException ex) {
ex.printStackTrace();
result = "N/A";
}
return result.trim();
}
3、获取CPU最小频率
[java] viewplain copy
// 获取CPU最小频率
public static String getMinCpuFreq() {
String result = "";
ProcessBuilder cmd;
try {
String[] args = { "/system/bin/cat",
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };
cmd = new ProcessBuilder(args);
Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[24];
while (in.read(re) != -1) {
result = result + new String(re);
}
in.close();
} catch (IOException ex) {
ex.printStackTrace();
result = "N/A";
}
return result.trim();
}
4、实时获取CPU当前频率
[java] viewplain copy
// 实时获取CPU当前频率
public static String getCurCpuFreq() {
String result = "N/A";
try {
FileReader fr = new FileReader(
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
BufferedReader br = new BufferedReader(fr);
String text = br.readLine();
result = text.trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//-----------------------------------------------------------------------------------------------------------------
直接使用命令来查看CPU的型号:
相关文章推荐
- android-destroyDrawingCache
- Java-Android【2】-弹出对话框
- ImageLoader源码分析
- Android实现推送方式解决方案
- gridview设置第一行距顶部的距离
- android提醒闹铃
- Android Studio 使用技巧
- 详谈Activity生命周期函数调用时机
- RecyclerView简单使用介绍
- SuperQQ小项目遇到的问题(1)
- Ecplise项目和AndroidStudio项目之间的转换
- Android Studio show line numbers
- Android Studio中的.gradle文件解读
- Android 5.x support.design.widget---Snackbar
- Android MVC
- android developer tiny share-20160714
- android developer tiny share-20160714
- Android自定义View制作动态炫酷按钮实例解析
- Android 编程小问题及Android studio小技巧
- Android 登录Web 时对cookie 处理