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
或者参考源码:
HardwarePropertiesManagerTest.java
使用 HardwarePropertiesManager 进行属性获取,但是这个需要 system 权限。
标签:temperature,temp,devices,class,sys,获取,thermal,android,cpu From: https://blog.51cto.com/u_15866638/8497614