Android系统信息获取 之三:CPU信息获取

(转载)http://blog.csdn.net/netwalk/article/details/9793501


Android系统的CPU信息涉及到两个文件:/proc/cpuinfo和/proc/stat 

通过读取文件/proc/cpuinfo,来获取系统CPU的类型等多种信息,

通过读取/proc/stat 所有CPU活动的信息来计算CPU使用率。


获取CPU信息可参考下面代码

1、 获取CPU名字

[java]  view plain  copy
  1. // 获取CPU名字  
  2.     public static String getCpuName() {  
  3.         try {  
  4.             FileReader fr = new FileReader("/proc/cpuinfo");  
  5.             BufferedReader br = new BufferedReader(fr);  
  6.             String text = br.readLine();  
  7.             String[] array = text.split(":\\s+"2);  
  8.             for (int i = 0; i < array.length; i++) {  
  9.             }  
  10.             return array[1];  
  11.         } catch (FileNotFoundException e) {  
  12.             e.printStackTrace();  
  13.         } catch (IOException e) {  
  14.             e.printStackTrace();  
  15.         }  
  16.         return null;  
  17.     }  

2、获取CPU最大频率

[java]  view plain  copy
  1. // 获取CPU最大频率  
  2.     // "/system/bin/cat" 命令行  
  3.     // "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" 存储最大频率的文件的路径  
  4.     public static String getMaxCpuFreq() {  
  5.         String result = "";  
  6.         ProcessBuilder cmd;  
  7.         try {  
  8.             String[] args = { "/system/bin/cat",  
  9.                     "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };  
  10.             cmd = new ProcessBuilder(args);  
  11.             Process process = cmd.start();  
  12.             InputStream in = process.getInputStream();  
  13.             byte[] re = new byte[24];  
  14.             while (in.read(re) != -1) {  
  15.                 result = result + new String(re);  
  16.             }  
  17.             in.close();  
  18.         } catch (IOException ex) {  
  19.             ex.printStackTrace();  
  20.             result = "N/A";  
  21.         }  
  22.         return result.trim();  
  23.     }  

3、获取CPU最小频率

[java]  view plain  copy
  1. // 获取CPU最小频率  
  2.     public static String getMinCpuFreq() {  
  3.         String result = "";  
  4.         ProcessBuilder cmd;  
  5.         try {  
  6.             String[] args = { "/system/bin/cat",  
  7.                     "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };  
  8.             cmd = new ProcessBuilder(args);  
  9.             Process process = cmd.start();  
  10.             InputStream in = process.getInputStream();  
  11.             byte[] re = new byte[24];  
  12.             while (in.read(re) != -1) {  
  13.                 result = result + new String(re);  
  14.             }  
  15.             in.close();  
  16.         } catch (IOException ex) {  
  17.             ex.printStackTrace();  
  18.             result = "N/A";  
  19.         }  
  20.         return result.trim();  
  21.     }  

4、实时获取CPU当前频率

[java]  view plain  copy
  1. // 实时获取CPU当前频率  
  2.     public static String getCurCpuFreq() {  
  3.         String result = "N/A";  
  4.         try {  
  5.             FileReader fr = new FileReader(  
  6.                     "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");  
  7.             BufferedReader br = new BufferedReader(fr);  
  8.             String text = br.readLine();  
  9.             result = text.trim();  
  10.         } catch (FileNotFoundException e) {  
  11.             e.printStackTrace();  
  12.         } catch (IOException e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.         return result;  
  16.     }  


//-----------------------------------------------------------------------------------------------------------------

直接使用命令来查看CPU的型号:


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值