如何获取CPU温度?

13

我知道在非root设备上可以通过某些方法获取CPU的温度,因为我下载了一款可以实现此功能的应用。请问如何实现这个功能?

编辑:这个应用叫做A1 CPU Tool。我花了几天的时间寻找答案。

编辑2:下面是我尝试过的代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    Sensor TempSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
    mSensorManager.registerListener(temperatureSensor, TempSensor, SensorManager.SENSOR_DELAY_FASTEST);
}

private SensorEventListener temperatureSensor = new SensorEventListener(){

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float temp = event.values[0];
        Log.i("sensor", "sensor temp = " + temp);
    }
};

当我这样做时,我发现TempSensor始终为空。根据文档,这意味着传感器不存在,因此我当然不会得到回调。您是否检查了TempSensor的空值? - Brian Reinhold
5个回答

12

阅读以下路径中的文件(因为不同设备路径不同),以获取来自不同设备的CPU温度详细信息,其中一个路径将返回所需的文件。

    "/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp"             
    "/sys/devices/system/cpu/cpu0/cpufreq/FakeShmoo_cpu_temp"    
    "/sys/class/thermal/thermal_zone1/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_zone0/temp"                      
    "/sys/devices/virtual/thermal/thermal_zone0/temp"            
    "/sys/class/hwmon/hwmon0/device/temp1_input"                 
    "/sys/devices/virtual/thermal/thermal_zone1/temp"            
    "/sys/devices/platform/s5p-tmu/curr_temp"    

为了读取文件,我使用了:

RandomAccessFile reader = new RandomAccessFile("/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp", "r");
String line = reader.readLine();

这里是另一种方法:读取系统文件


1
如果您提供了如何读取路径的解释的参考,那将是非常好的,这样就可以在编程中使用它。 - Cornelius Roemer
1
这是一篇描述如何使用文件读取器的帖子:https://dev59.com/8H7aa4cB1Zd3GeqPkwfQ#22659958 - Cornelius Roemer
1
@CorneliusRoemer 我刚刚使用了RandomAccessFile。 - Jyoti JK
1
不错!这比我找到的任何其他替代方案都要简洁得多。感谢您的编辑。如果您愿意,您还可以添加我发现的这些额外文件路径:/sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/ext_temperature /sys/devices/platform/tegra-tsensor/tsensor_temperature``` - Cornelius Roemer

6
如果设备没有通过温度传感器公开CPU温度(也就是说,请求类型为TEMPERATUREAMBIENT_TEMPERATURE的传感器不会给出任何东西),则您将无法以标准方式读取它。当然,这并不代表不能用非标准的方式读取,如果您的应用程序不需要广泛分发,则这种方式可能是适合您的。
如果您知道在哪里查找,可以在/sys/devices/platform中读取设备文件。如果您运行adb shellcd进入该文件夹,则有相当大的机会find | grep temp给出代表温度传感器的文件。
这些文件不仅仅是CPU的 - 例如,我的Google Glass的电池温度传感器可以通过读取/sys/devices/platform/omap_i2c.1/i2c-1/1-0055/power_supply/bq27520-0/temp来访问。
看看这条路径,就可以清楚地知道为什么您不应该这样做并期望它在所有设备上都能正常工作 - 路径以及文件的格式也会因设备而异。但是,在紧急情况下,这是一种快速而简单的方法来完成工作。

我在使用sys/devices/virtual/thermal/thermal_zone0/temp时取得了最大的成功。请参考https://dev59.com/VmAf5IYBdhLWcg3wUBNc。 - ban-geoengineering

3

http://repo.xposed.info/module/org.narcotek.cputemp

请仔细阅读以下内容,我认为它可能会对你有所帮助。

我正在使用一个名为“Cool Tool”的应用程序;它非常棒,具有许多功能...然后我启用了自定义标签1,并按照链接中的说明进行操作。我将路径更改为/sys/class/thermal/thermal_zone0/temp,还有这个选项我没有更改:refer:(/d+)/d{3},以及替换模式:1$。如果需要,请使用Cool Tool!


3

以下是2019年12月生产中用于查找CPU温度的当前路径集合。由于不同的OEM将温度放置在不同的位置,因此存在许多不同的路径。

除上述路径外,还会使用以下三个位置:

/sys/htc/cpu_temp
/sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/ext_temperature
/sys/devices/platform/tegra-tsensor/tsensor_temperature

之前已经列出的答案仍然有效:
/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp
/sys/devices/system/cpu/cpu0/cpufreq/FakeShmoo_cpu_temp
/sys/class/thermal/thermal_zone1/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_zone0/temp
/sys/devices/virtual/thermal/thermal_zone0/temp
/sys/class/hwmon/hwmon0/device/temp1_input
/sys/devices/virtual/thermal/thermal_zone1/temp
/sys/devices/platform/s5p-tmu/curr_temp

这里有一个代码片段,演示如何读取系统文件。这是经典的Unix哲学“一切皆为文件”,即使在此情况下传感器读数也是通过读取“文件”来访问的。
RandomAccessFile reader = new RandomAccessFile("/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp", "r");
String line = reader.readLine();

结果很可能是以毫摄氏度为单位的,例如33000代表33摄氏度,因此您需要将其除以1000才能得到摄氏度温度。


测试了在OnePlus 7 Pro Android 10上,以下工作:/sys/class/thermal/thermal_zone1/temp,/sys/class/thermal/thermal_zone0/temp,/sys/devices/virtual/thermal/thermal_zone0/temp。但是写入这些文件的值类似于一个大数,即33000。鉴于我同时使用了Cpu Monitor应用程序,显示33摄氏度,我认为它们读取相同的文件,但将数字除以可读格式。 - Peter
@Peter,是的,读数是以mC为单位的,所以需要除以1000。 - Cornelius Roemer

-1

2
我已经在我的 Nexus 4 上尝试了这两个(工具),但都没有起作用。 - Asaf
@AsafSchreiber 你确定你正确地尝试了吗?需要有人发布一些代码;) - Greg Giacovelli
2
我应该提到,我尝试了使用TYPE_AMBIENT_TEMPERATURE和TYPE_TEMPERATURE这两种传感器类型的完全相同的代码,但在两种情况下,日志中都没有任何消息(意味着onSensorChanged函数从未运行)。 - Asaf
@Asaf,你是如何解决这个问题的?我也遇到了同样的问题,我的设备上没有任何温度传感器可用。 - Erum

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接