简体   繁体   中英

How I can get Hardware Information in Android app

I want to get information about a device such as RAM (mem size), GPU (model, mem size), CPU (model, count of cores) programmatically in android app(kotlin). I found some similar posts (for example that ), but there is no solution there. Help me please

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

You can use this -

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类获得。

I've been looking around android files and what they contain and found a way to get cpu name and available ram memory size, but did not found any way to get to the gpu name

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

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

This probably won't help much, but still might help someone in the future

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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