首页 > 解决方案 > 从 android 设备获取 CPU 温度

问题描述

我正在尝试获取设备的温度。我使用了环境传感器(环境温度),但这不适用于大多数设备。我还使用了诸如“sys/class/thermal/thermal_zone0/temp”之类的命令,但没有运气

请让我知道是否有人能够访问最新设备(例如(Samsung S10)、Vivi 1938)上的温度

标签: androidcpu-usageandroid-sensorsandroid-10.0temperature

解决方案


您可以使用以下代码获取设备的 CPU 温度:

public static float cpuTemperature()
{
  Process process;
  try {
    process = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone0/temp");
    process.waitFor();
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = reader.readLine();
    if(line!=null) {
        float temp = Float.parseFloat(line);
        return temp / 1000.0f;
    }else{
        return 51.0f;
    }
 } catch (Exception e) {
    e.printStackTrace();
    return 0.0f;
 }
}

推荐阅读