android获取手机cpu并判断是单核还是多核
程序员文章站
2023-11-30 23:21:58
复制代码 代码如下:/** * gets the number of cores available in this device, across all processo...
复制代码 代码如下:
/**
* 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;
}
}