因为Android本质上是linux系统,所以关系硬件相关的信息实际上是用文件表示的
CPU相关信息放在 /sys/devices/system/cpu/
这个路径下,
要知道cpu的核数只需要查看这个路径中的文件即可
如果CPU是双核的,则 /sys/devices/system/cpu/
路径下有两个子文件夹cpu0
和cpu1
如果CPU是四核的,则 /sys/devices/system/cpu/
路径下有四个子文件夹cpu0
和cpu1
和cpu2
和cpu3
以此类推
用JAVA代码来获取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; //上面的意思就是2.3以前不支持多核,有些特殊的设备有双核...不考虑,就当单核!!
}
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;
}
};