如何从代码中检测Android设备上的双核cpu?

new*_*yca 20 android

我遇到的问题似乎只影响运行姜饼或更大的双核Android设备.我想仅针对符合该标准的用户提供有关此问题的对话框.我知道如何检查操作系统级别,但没有找到任何可以明确告诉我设备使用多核的东西.

有任何想法吗?

Dav*_*vid 43

不幸的是,对于大多数Android设备,availableProcessors()方法无法正常工作.偶数/ proc/stat并不总是显示正确的CPU数量.

我发现确定CPU数量的唯一可靠方法是枚举/ sys/devices/system/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 one or more digits
            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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这个Java代码应该适用于任何Android应用程序,即使没有root.


小智 8

如果您正在使用本机应用程序,则应尝试以下操作:

#include <unistd.h>
int GetNumberOfProcessor()
{
    return sysconf(_SC_NPROCESSORS_CONF);
}
Run Code Online (Sandbox Code Playgroud)

它适用于我的i9100(availableProcessors()返回1).