你怎么能检测到双核CPU从code在Android设备上? [英] How can you detect a dual-core cpu on an Android device from code?

查看:112
本文介绍了你怎么能检测到双核CPU从code在Android设备上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到的,似乎影响运行姜饼或更高版本唯一的双核Android设备有问题。我想只有我的用户适合这个标准给有关此问题的对话框。我知道如何检查操作系统级别,但还没有找到任何可以明确地告诉我,使用多核器件。

I've run into a problem that appears to effect only dual-core Android devices running gingerbread or greater. I'd like to give a dialog regarding this issue only to my users that fit that criteria. I know how to check OS level, but haven't found anything that can definitively tell me the device is using multi-core.

任何想法?

推荐答案

不幸的是,大多数Android设备上,availableProcessors()方法无法正常工作。即使是的/ proc / STAT并不总是显示CPU的正确数量。

Unfortunately for most Android devices, the availableProcessors() method doesn't work correctly. Even /proc/stat doesn't always show the correct number of CPUs.

的唯一可靠的方法,我发现,以确定CPU的数量是枚举虚拟CPU的名单上/ SYS /设备/系统/ CPU / <一href="http://forums.makingmoneywithandroid.com/android-development/280-%5Bhow-%5D-get-number-cpu-cores-android-device.html">as在这个论坛帖子描述。在code:

The only reliable method I've found to determine the number of CPUs is to enumerate the list of virtual CPUs at /sys/devices/system/cpu/ as described in this forum post. The code:

/**
 * 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;
    }
}

本的Java code应该在所有的Andr​​oid应用程序,即使没有根。

This Java code should work in any Android application, even without root.

这篇关于你怎么能检测到双核CPU从code在Android设备上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆