获得android手机的硬件信息

时间:2022-09-08 19:04:39
//CPU个数
private int getNumCores() {
//Private Class to display only CPU devices in the directory listing
class CpuFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
//Check if filename is "cpu", followed by a single digit number
if(Pattern.matches("cpu[0-9]", pathname.getName())) {
return true;
}
return false;
}
}

try {
//Get directory containing CPU info
File dir = new File("/sys/devices/system/cpu/");
//Filter to only list the devices we care about
File[] files = dir.listFiles(new CpuFilter());
Log.d(TAG,
"CPU Count: "+files.length);
//Return the number of cores (virtual CPU devices)
return files.length;
}
catch(Exception e) {
//Print exception
Log.d(TAG, "CPU Count: Failed.");
e.printStackTrace();
//Default to return 1 core
return 1;
}
}


不需要root权限。

/sys/devices/system/cpu/cpu0 ----------Cpu1

/sys/devices/system/cpu/cpu1 ----------Cpu2

获取CPU最大频率

     // "/system/bin/cat" 命令行
     // "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" 存储最大频率的文件的路径

[java] view plaincopy
  1. public static long getCpuFrequence() {  
  2.         ProcessBuilder cmd;  
  3.         try {  
  4.             String[] args = { "/system/bin/cat",  
  5.                     "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };  
  6.             cmd = new ProcessBuilder(args);  
  7.   
  8.             Process process = cmd.start();  
  9.             BufferedReader reader = new BufferedReader(new InputStreamReader(  
  10.                     process.getInputStream()));  
  11.             String line = reader.readLine();  
  12.             return StringUtils.parseLongSafe(line, 100);  
  13.         } catch (IOException ex) {  
  14.             ex.printStackTrace();  
  15.         }  
  16.         return 0;  
  17.     }  

// 获取CPU最小频率(单位KHZ):

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

// 实时获取CPU当前频率(单位KHZ):

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

/ 获取CPU名字:

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

内存:/proc/meminfo:

[java] view plaincopy
  1. public void getTotalMemory() {    
  2.         String str1 = "/proc/meminfo";    
  3.         String str2="";    
  4.         try {    
  5.             FileReader fr = new FileReader(str1);    
  6.             BufferedReader localBufferedReader = new BufferedReader(fr, 8192);    
  7.             while ((str2 = localBufferedReader.readLine()) != null) {    
  8.                 Log.i(TAG, "---" + str2);    
  9.             }    
  10.         } catch (IOException e) {    
  11.         }    
  12.     }   

Rom大小 www.2cto.com

[java] view plaincopy
  1. public long[] getRomMemroy() {    
  2.         long[] romInfo = new long[2];    
  3.         //Total rom memory    
  4.         romInfo[0] = getTotalInternalMemorySize();    
  5.      
  6.         //Available rom memory    
  7.         File path = Environment.getDataDirectory();    
  8.         StatFs stat = new StatFs(path.getPath());    
  9.         long blockSize = stat.getBlockSize();    
  10.         long availableBlocks = stat.getAvailableBlocks();    
  11.         romInfo[1] = blockSize * availableBlocks;    
  12.         getVersion();    
  13.         return romInfo;    
  14.     }    
  15.      
  16.     public long getTotalInternalMemorySize() {    
  17.         File path = Environment.getDataDirectory();    
  18.         StatFs stat = new StatFs(path.getPath());    
  19.         long blockSize = stat.getBlockSize();    
  20.         long totalBlocks = stat.getBlockCount();    
  21.         return totalBlocks * blockSize;    
  22.     }   

sdCard大小:

[java] view plaincopy
  1. public long[] getSDCardMemory() {    
  2.         long[] sdCardInfo=new long[2];    
  3.         String state = Environment.getExternalStorageState();    
  4.         if (Environment.MEDIA_MOUNTED.equals(state)) {    
  5.             File sdcardDir = Environment.getExternalStorageDirectory();    
  6.             StatFs sf = new StatFs(sdcardDir.getPath());    
  7.             long bSize = sf.getBlockSize();    
  8.             long bCount = sf.getBlockCount();    
  9.             long availBlocks = sf.getAvailableBlocks();    
  10.      
  11.             sdCardInfo[0] = bSize * bCount;//总大小    
  12.             sdCardInfo[1] = bSize * availBlocks;//可用大小    
  13.         }    
  14.         return sdCardInfo;    
  15.     }   

系统的版本信息:

[java] view plaincopy
  1. public String[] getVersion(){    
  2.     String[] version={"null","null","null","null"};    
  3.     String str1 = "/proc/version";    
  4.     String str2;    
  5.     String[] arrayOfString;    
  6.     try {    
  7.         FileReader localFileReader = new FileReader(str1);    
  8.         BufferedReader localBufferedReader = new BufferedReader(    
  9.                 localFileReader, 8192);    
  10.         str2 = localBufferedReader.readLine();    
  11.         arrayOfString = str2.split("\\s+");    
  12.         version[0]=arrayOfString[2];//KernelVersion    
  13.         localBufferedReader.close();    
  14.     } catch (IOException e) {    
  15.     }    
  16.     version[1] = Build.VERSION.RELEASE;// firmware version    
  17.     version[2]=Build.MODEL;//model    
  18.     version[3]=Build.DISPLAY;//system version    
  19.     return version;    
  20. }   

mac地址和开机时间

[java] view plaincopy
  1. public String[] getOtherInfo(){    
  2.     String[] other={"null","null"};    
  3.        WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);    
  4.        WifiInfo wifiInfo = wifiManager.getConnectionInfo();    
  5.        if(wifiInfo.getMacAddress()!=null){    
  6.         other[0]=wifiInfo.getMacAddress();    
  7.     } else {    
  8.         other[0] = "Fail";    
  9.     }    
  10.     other[1] = getTimes();    
  11.        return other;    
  12. }    
  13. private String getTimes() {    
  14.     long ut = SystemClock.elapsedRealtime() / 1000;    
  15.     if (ut == 0) {    
  16.         ut = 1;    
  17.     }    
  18.     int m = (int) ((ut / 60) % 60);    
  19.     int h = (int) ((ut / 3600));    
  20.     return h + " " + mContext.getString(R.string.info_times_hour) + m + " "   
  21.             + mContext.getString(R.string.info_times_minute);    
  22. }   
---------------------------------------------------

[java] view plaincopy
  1. private static void updateCpuStat() {  
  2.     ProcessBuilder cmd;  
  3.     long oldMypidStat = first ? 0 : mypidStat;  
  4.     long oldTotal = first ? 0 : total;  
  5.   
  6.     try {  
  7.         StringBuilder builder = new StringBuilder();  
  8.         int pid = android.os.Process.myPid();  
  9.         String[] args = { "/system/bin/cat""/proc/" + pid + "/stat" };  
  10.         cmd = new ProcessBuilder(args);  
  11.   
  12.         Process process = cmd.start();  
  13.         InputStream in = process.getInputStream();  
  14.         byte[] re = new byte[1024];  
  15.         int len;  
  16.         while ((len = in.read(re)) != -1) {  
  17.             builder.append(new String(re, 0, len));  
  18.         }  
  19.         // Log.i(TAG, builder.toString());  
  20.         in.close();  
  21.   
  22.         String[] stats = builder.toString().split(" +", -1);  
  23.         if (stats.length >= 17) {  
  24.             long utime = StringUtils.parseLongSafe(stats[13], 10);  
  25.             long stime = StringUtils.parseLongSafe(stats[14], 10);  
  26.             long cutime = StringUtils.parseLongSafe(stats[15], 10);  
  27.             long cstime = StringUtils.parseLongSafe(stats[16], 10);  
  28.             // Log.i(TAG, String.format(  
  29.             // "utime:%d, stime:%d, cutime:%d, cstime:%d", utime,  
  30.             // stime, cutime, cstime));  
  31.             mypidStat = utime + stime + cutime + cstime;  
  32.         }  
  33.     } catch (IOException ex) {  
  34.         ex.printStackTrace();  
  35.     }  
  36.   
  37.     try {  
  38.         String[] args = { "/system/bin/cat""/proc/stat" };  
  39.         cmd = new ProcessBuilder(args);  
  40.   
  41.         Process process = cmd.start();  
  42.         BufferedReader reader = new BufferedReader(new InputStreamReader(  
  43.                 process.getInputStream()));  
  44.         String line = null;  
  45.         while ((line = reader.readLine()) != null) {  
  46.             Matcher matcher = pattern.matcher(line);  
  47.             if (matcher.matches()) {  
  48.                 String[] stats = line.split(" +", -1);  
  49.                 long tmpTotal = 0;  
  50.                 for (int i = 1; i < stats.length; i++) {  
  51.                     tmpTotal += StringUtils.parseLongSafe(stats[i], 10);  
  52.                 }  
  53.                 total = tmpTotal;  
  54.             }  
  55.             break;  
  56.         }  
  57.         if (first) {  
  58.             first = false;  
  59.         } else {  
  60.             Log.i(TAG, String.format("vsir cpu usage: %2.1f%%",  
  61.                     (double) (mypidStat - oldMypidStat)  
  62.                             / (total - oldTotal) * 100));  
  63.         }  
  64.         reader.close();  
  65.     } catch (IOException ex) {  
  66.         ex.printStackTrace();  
  67.     }  
  68. }