當前位置:編程學習大全網 - 編程軟體 - android獲取手機cpu並判斷是單核還是多核

android獲取手機cpu並判斷是單核還是多核

android的Cpu信息是存在/sys/devices/system/cpu中的,在目錄中,我們可以看到存在多個文件,壹個文件就是壹核Cpu的信息。上面寫有cpu0,cpu1,cup3諸如此類的文件夾。

要獲得它是幾核的Cpu,只要讀取壹下那個cpu目錄下有幾個cpu+數字的文件夾就可以了。

/**

* Gets the number of cores available in this device, across all processors.

* Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"

* @return The number of cores, or 1 if failed to get result

*/

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());

//Return the number of cores (virtual CPU devices)

return files.length;

} catch(Exception e) {

//Default to return 1 core

return 1;

}

}

  • 上一篇:matlab最小二乘法程序有道題需大家解答 x=19 25 31 38 44 y=19.0 32.3 49.0 73.3 97.8求形如y=a+bx^2中a,b
  • 下一篇:晚上家裏沒人日了媽媽
  • copyright 2024編程學習大全網