當前位置:編程學習大全網 - 源碼下載 - 如何獲取 Android 設備的CPU核數,時鐘頻率以及內存大小

如何獲取 Android 設備的CPU核數,時鐘頻率以及內存大小

獲取 CPU 核數Linux 中的設備都是以文件的形式存在,CPU 也不例外,因此 CPU 的文件個數就等價與核數。Android 的 CPU 設備文件位於/sys/devices/system/cpu/目錄,文件名的的格式為cpu\d+。root@generic_x86_64:/sys/devices/system/cpu # ls cpu0 cpufreqcpuidlekernel_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 Gingerbread; that can let an app in the background run without impacting // the foreground application. But for our purposes, it makes them single core. return 1; } int cores; try { cores = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length; } catch (SecurityException e) { cores = DEVICEINFO_UNKNOWN; } catch (NullPointerException e) { cores = DEVICEINFO_UNKNOWN; } return cores;}private static final FileFilter CPU_FILTER = new FileFilter() { @Override 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') { return false; } } return true; } return false; }};獲取時鐘頻率獲取時鐘頻率需要讀取系統文件 -/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq或者/proc/cpuinfo。 Android 模擬器中並沒有cpuinfo_max_freq文件,因此只能讀取/proc/cpuinfo。/proc/cpuinfo包含了很多 cpu 數據。processor : 0vendor_id : GenuineIntelcpu family : 6model : 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 : nofpu : yesfpu_exception : yescpuid level : 4wp : yes代碼如下:public static int getCPUMaxFreqKHz() { int maxFreq = DEVICEINFO_UNKNOWN; try { 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); try { 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 = freqBound; } catch (NumberFormatException e) { //Fall through and use /proc/cpuinfo. } finally { stream.close(); } } } if (maxFreq == DEVICEINFO_UNKNOWN) { FileInputStream stream = new FileInputStream("/proc/cpuinfo"); try { int freqBound = parseFileForValue("cpu MHz", stream); freqBound *= 1000; //MHz -> kHz if (freqBound > maxFreq) maxFreq = freqBound; } finally { stream.close(); } } } catch (IOException e) { maxFreq = DEVICEINFO_UNKNOWN; //Fall through and return unknown. } return maxFreq;}獲取內存大小如果 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.totalMem; } else { return DEVICEINFO_UNKNOWN; } } else { long totalMem = DEVICEINFO_UNKNOWN; try { FileInputStream stream = new FileInputStream("/proc/meminfo"); try { totalMem = parseFileForValue("MemTotal", stream); totalMem *= 1024; } finally { stream.close(); } } catch (IOException e) { } return totalMem; }}

  • 上一篇:幽門螺桿菌的檢測指標正常值是多少,具體
  • 下一篇:Waf源代碼
  • copyright 2024編程學習大全網