Android下获取CPU信息的办法

来源:互联网 发布:up to date数据库 编辑:程序博客网 时间:2024/06/04 18:02

1、最简单的是使用Build的信息。

import android.os.Build;
String cpu = Build.CPU_ABI;
cpu.startWith("arm");

2、使用系统文件/proc/cpuinfo

  1. try {  
  2.         InputStream is = new FileInputStream("/proc/cpuinfo");  
  3.         InputStreamReader ir = new InputStreamReader(is);  
  4.         BufferedReader br = new BufferedReader(ir);  
  5.         try {  
  6.             String nameProcessor = "Processor";  
  7.             String nameFeatures = "Features";  
  8.             String nameModel = "model name";  
  9.             String nameCpuFamily = "cpu family";  
  10.             while (true) {  
  11.                 String line = br.readLine();  
  12.                 String[] pair = null;  
  13.                 if (line == null) {  
  14.                     break;  
  15.                 }  
  16.                 pair = line.split(":");  
  17.                 if (pair.length != 2)  
  18.                     continue;  
  19.                 String key = pair[0].trim();  
  20.                 String val = pair[1].trim();  
  21.                 if (key.compareTo(nameProcessor) == 0) {  
  22.                     String n = "";  
  23.                     for (int i = val.indexOf("ARMv") + 4; i < val.length(); i++) {  
  24.                         String temp = val.charAt(i) + "";  
  25.                         if (temp.matches("\\d")) {  
  26.                             n += temp;  
  27.                         } else {  
  28.                             break;  
  29.                         }  
  30.                     }  
  31.                     mArmArchitecture[0] = "ARM";  
  32.                     mArmArchitecture[1] = Integer.parseInt(n);  
  33.                     continue;  
  34.                 }  
  35.   
  36.                 if (key.compareToIgnoreCase(nameFeatures) == 0) {  
  37.                     if (val.contains("neon")) {  
  38.                         mArmArchitecture[2] = "neon";  
  39.                     }  
  40.                     continue;  
  41.                 }  
  42.   
  43.                 if (key.compareToIgnoreCase(nameModel) == 0) {  
  44.                     if (val.contains("Intel")) {  
  45.                         mArmArchitecture[0] = "INTEL";  
  46.                         mArmArchitecture[2] = "atom";  
  47.                     }  
  48.                     continue;  
  49.                 }  
  50.   
  51.                 if (key.compareToIgnoreCase(nameCpuFamily) == 0) {  
  52.                     mArmArchitecture[1] = Integer.parseInt(val);  
  53.                     continue;  
  54.                 }  
  55.             }  
  56.         } finally {  
  57.             br.close();  
  58.             ir.close();  
  59.             is.close();  
  60.         }  
  61.     } catch (Exception e) {  
  62.         e.printStackTrace();  
  63.     } 

0 0
原创粉丝点击