繁体   English   中英

如何在 Android 应用中获取硬件信息

[英]How I can get Hardware Information in Android app

我想在 android 应用程序(kotlin)中以编程方式获取有关设备的信息,例如 RAM(内存大小)、GPU(模型、内存大小)、CPU(模型、内核数)。 我发现了一些类似的帖子(例如that ),但那里没有解决方案。 请帮帮我

            **FOR RAM**     
            private val activityManager: ActivityManager
                 fun getTotalBytes(): Long {
                        val memoryInfo = ActivityManager.MemoryInfo()
                        activityManager.getMemoryInfo(memoryInfo)
                        return memoryInfo.totalMem
                    }
                
                    fun getAvailableBytes(): Long {
                        val memoryInfo = ActivityManager.MemoryInfo()
                        activityManager.getMemoryInfo(memoryInfo)
                        return memoryInfo.availMem
                    }
                
                    fun getAvailablePercentage(): Int {
                        val total = getTotalBytes().toDouble()
                        val available = getAvailableBytes().toDouble()
                        return (available / total * 100).toInt()
                    }
                
                    fun getThreshold(): Long {
                        val memoryInfo = ActivityManager.MemoryInfo()
                        activityManager.getMemoryInfo(memoryInfo)
                        return memoryInfo.threshold
        **FOR CPU**
     private const val CPU_INFO_DIR = "/sys/devices/system/cpu/"
          fun getNumberOfCores(): Int {
                return if (Build.VERSION.SDK_INT >= 17) {
                    Runtime.getRuntime().availableProcessors()
                } else {
                    getNumCoresLegacy()
                }
            }
        
            /**
             * Checking frequencies directories and return current value if exists (otherwise we can
             * assume that core is stopped - value -1)
             */
            fun getCurrentFreq(coreNumber: Int): Long {
                val currentFreqPath = "${CPU_INFO_DIR}cpu$coreNumber/cpufreq/scaling_cur_freq"
                return try {
                    RandomAccessFile(currentFreqPath, "r").use { it.readLine().toLong() / 1000 }
                } catch (e: Exception) {
                    Timber.e("getCurrentFreq() - cannot read file")
                    -1
                }
            }
        
            /**
             * Read max/min frequencies for specific [coreNumber]. Return [Pair] with min and max frequency
             * or [Pair] with -1.
             */
            fun getMinMaxFreq(coreNumber: Int): Pair<Long, Long> {
                val minPath = "${CPU_INFO_DIR}cpu$coreNumber/cpufreq/cpuinfo_min_freq"
                val maxPath = "${CPU_INFO_DIR}cpu$coreNumber/cpufreq/cpuinfo_max_freq"
                return try {
                    val minMhz = RandomAccessFile(minPath, "r").use { it.readLine().toLong() / 1000 }
                    val maxMhz = RandomAccessFile(maxPath, "r").use { it.readLine().toLong() / 1000 }
                    Pair(minMhz, maxMhz)
                } catch (e: Exception) {
                    Timber.e("getMinMaxFreq() - cannot read file")
                    Pair(-1, -1)
                }
            }

  private fun getNumCoresLegacy(): Int {
        class CpuFilter : FileFilter {
            override fun accept(pathname: File): Boolean {
                // Check if filename is "cpu", followed by a single digit number
                if (Pattern.matches("cpu[0-9]+", pathname.name)) {
                    return true
                }
                return false
            }
        }
        return try {
            File(CPU_INFO_DIR).listFiles(CpuFilter())!!.size
        } catch (e: Exception) {
            1
        }
    }
    
        **FOR GPU**
    
            fun getGlEsVersion(): String {
                return activityManager.deviceConfigurationInfo.glEsVersion
            }

你可以使用这个 -

Log.i("TAG", "SERIAL: " + Build.SERIAL);
Log.i("TAG","MODEL: " + Build.MODEL);
Log.i("TAG","ID: " + Build.ID);
Log.i("TAG","Manufacture: " + Build.MANUFACTURER);
Log.i("TAG","brand: " + Build.BRAND);
Log.i("TAG","type: " + Build.TYPE);
Log.i("TAG","user: " + Build.USER);
Log.i("TAG","BASE: " + Build.VERSION_CODES.BASE);
Log.i("TAG","INCREMENTAL " + Build.VERSION.INCREMENTAL);
Log.i("TAG","SDK  " + Build.VERSION.SDK);
Log.i("TAG","BOARD: " + Build.BOARD);
Log.i("TAG","BRAND " + Build.BRAND);
Log.i("TAG","HOST " + Build.HOST);
Log.i("TAG","FINGERPRINT: "+Build.FINGERPRINT);
Log.i("TAG","Version Code: " + Build.VERSION.RELEASE);

其中一些信息可通过android.os.Build类获得。

我一直在寻找 android 文件及其包含的内容,并找到了一种获取 cpu 名称和可用 ram 内存大小的方法,但没有找到任何获取 gpu 名称的方法

    fun fetchCpuName(): String {
        return File("/proc/cpuinfo").readLines().last() // Cpu name
    }
    

    fun fetchRamInfo(): String {
        return File("/proc/meminfo").readLines().first() // MemTotal
    }

这可能无济于事,但将来仍可能对某人有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM