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

发布于 2025-02-09 17:14:19 字数 252 浏览 2 评论 0原文

我想在Android App(Kotlin)中以编程方式获取有关RAM(MEM大小),GPU(模型,MEM大小),CPU(模型,内核计数)等设备的信息。我发现了一些类似的帖子(例如 that ),但是那里没有解决方案。请帮我

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慈悲佛祖 2025-02-16 17:14:19
            **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
            }
            **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
            }
中性美 2025-02-16 17:14:19

我一直在环顾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
    }

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

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

﹏雨一样淡蓝的深情 2025-02-16 17:14:19

您可以使用此 -

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);

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);
温折酒 2025-02-16 17:14:19

其中一些信息可通过 android。 OS.build类。

Some of this information is available via the android.os.Build class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文