How can you detect a dual-core cpu on an Android device from code?

前端 未结 6 1826
小鲜肉
小鲜肉 2020-11-29 23:42

I\'ve run into a problem that appears to affect only dual-core Android devices running Android 2.3 (Gingerbread or greater. I\'d like to give a di

6条回答
  •  抹茶落季
    2020-11-29 23:54

    Here's my solution, in Kotlin, based on this one:

            /**
             * return the number of cores of the device.
             * based on : http://stackoverflow.com/a/10377934/878126
             */
            private var coresCount: Int = 0
                get() {
                    if (field > 0)
                        return field
                    class CpuFilter : FileFilter {
                        override fun accept(pathname: File): Boolean {
                            return Pattern.matches("cpu[0-9]+", pathname.name)
                        }
                    }
                    try {
                        val dir = File("/sys/devices/system/cpu/")
                        val files = dir.listFiles(CpuFilter())
                        if (files != null) {
                            field = files.size
                            return field
                        }
                    } catch (ignored: Exception) {
                    }
                    field = max(1, Runtime.getRuntime().availableProcessors())
                    return field
                }
    

提交回复
热议问题