android 获取cpu温度

Android 进阶 专栏收录该内容
54 篇文章 0 订阅

1、实力展示

 

 

 

2、核心代码分析

 

就是读取指定文件下,一些cpu信息文件,然后,对里面的数据进行筛选。

有的机器上没有温度显示,大概率是下面的文件列表中,在当前机器上没有读权限,或者就直接没有列表中的文件。

  /**
     * Scan device looking for CPU temperature in all well known locations
     */
    fun getCpuTemperatureFinder(): Maybe<CpuTemperatureResult> {
        return Observable.fromIterable(CPU_TEMP_FILE_PATHS)
                .map { path ->
                    val temp = Utils.readOneLine(File(path))
                    var validPath = ""
                    var currentTemp = 0.0
                    if (temp != null) {
                        // Verify if we are in normal temperature range
                        if (isTemperatureValid(temp)) {
                            validPath = path
                            currentTemp = temp
                        } else if (isTemperatureValid(temp / 1000)) {
                            validPath = path
                            currentTemp = temp / 1000
                        }
                    }
                    CpuTemperatureResult(validPath, currentTemp.toInt())
                }
                .filter { (filePath) -> !filePath.isEmpty() }
                .firstElement()
    }

    /**
     * Check if passed temperature is in normal range: -30 - 250 Celsius
     *
     * @param temp current temperature
     */
    private fun isTemperatureValid(temp: Double): Boolean = temp in -30.0..250.0

    /**
     * Container for temperature value and path
     */
    data class CpuTemperatureResult(val filePath: String = "", val temp: Int = 0)

    companion object {
        // Ugly but currently the easiest working solution is to search well known locations
        // If you know better solution please refactor this :)
        private val CPU_TEMP_FILE_PATHS = listOf(
                "/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp",
                "/sys/devices/system/cpu/cpu0/cpufreq/FakeShmoo_cpu_temp",
                "/sys/class/thermal/thermal_zone0/temp",
                "/sys/class/i2c-adapter/i2c-4/4-004c/temperature",
                "/sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/temperature",
                "/sys/devices/platform/omap/omap_temp_sensor.0/temperature",
                "/sys/devices/platform/tegra_tmon/temp1_input",
                "/sys/kernel/debug/tegra_thermal/temp_tj",
                "/sys/devices/platform/s5p-tmu/temperature",
                "/sys/class/thermal/thermal_zone1/temp",
                "/sys/class/hwmon/hwmon0/device/temp1_input",
                "/sys/devices/virtual/thermal/thermal_zone1/temp",
                "/sys/devices/virtual/thermal/thermal_zone0/temp",
                "/sys/class/thermal/thermal_zone3/temp",
                "/sys/class/thermal/thermal_zone4/temp",
                "/sys/class/hwmon/hwmonX/temp1_input",
                "/sys/devices/platform/s5p-tmu/curr_temp")
    }

 

源码来自 github 

或者 csdn下载

或者参考源码:

HardwarePropertiesManagerTest.java

使用   HardwarePropertiesManager 进行属性获取,但是这个需要 system 权限。

 

 

  • 1
    点赞
  • 2
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值