【问题标题】:how to get android cpu temperature programmatically如何以编程方式获取android cpu温度
【发布时间】:2018-08-21 12:29:11
【问题描述】:

试过这个但得到 0.0 并且在物理设备上没有找到.. 在android中获取cpu温度的任何方法

SensorManager mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    Sensor AmbientTemperatureSensor
            = mySensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
    if (AmbientTemperatureSensor != null) {
        mySensorManager.registerListener(
                AmbientTemperatureSensorListener,
                AmbientTemperatureSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    }

私有最终 SensorEventListener AmbientTemperatureSensorListener = 新的 SensorEventListener() {

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

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
            temperature = event.values[0];
            Messages.sendMessage(getApplicationContext(),Float.toString(temperature));
        }
    }

};

【问题讨论】:

标签: android cpu temperature


【解决方案1】:
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;
    }
}

【讨论】:

  • SELinux 问题。权限被拒绝。
  • 这个值是摄氏度还是华氏度?
  • 我不记得了,已经有一段时间了,但我认为它已经很远了。
  • 从 adb shell 我得到正确的 temp 值。但使用 Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone0/temp");我越来越空了。
【解决方案2】:

您可以从此代码中找到所有热值(温度和类型)(不仅是 CPU 温度)。还要记住,sys/class/thermal/thermal_zone0/temp 并不总是指向 CPU 温度(在我的情况下,它指向的是电池温度)。始终在后台线程中使用此代码。我已经在真机和模拟器上测试过,效果很好。

public void thermal() {
        String temp, type;
        for (int i = 0; i < 29; i++) {
            temp = thermalTemp(i);
            if (!temp.contains("0.0")) {
                type = thermalType(i);
                if (type != null) {
                   System.out.println("ThermalValues "+type+" : "+temp+"\n"); 
                }
            }
        }
    }

    public String thermalTemp(int i) {
        Process process;
        BufferedReader reader;
        String line;
        String t = null;
        float temp = 0;
        try {
            process = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone" + i + "/temp");
            process.waitFor();
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            line = reader.readLine();
            if (line != null) {
                temp = Float.parseFloat(line);
            }
            reader.close();
            process.destroy();
            if (!((int) temp == 0)) {
                if ((int) temp > 10000) {
                    temp = temp / 1000;
                } else if ((int) temp > 1000) {
                    temp = temp / 100;
                } else if ((int) temp > 100) {
                    temp = temp / 10;
                }
            } else
                t = "0.0";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public String thermalType(int i) {
        Process process;
        BufferedReader reader;
        String line, type = null;
        try {
            process = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone" + i + "/type");
            process.waitFor();
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            line = reader.readLine();
            if (line != null) {
                type = line;
            }
            reader.close();
            process.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return type;
    }

Logcat 中的示例输出(下图是真实设备输出...在模拟器上它只显示类型 battery 及其温度。):

【讨论】:

  • 这个列表会包含Shutdown Temperature 吗?
  • 可能因设备而异。我不太确定Shutdown Temperature。但这是获得温度的唯一可能方法。
  • 你忘记了 t=Integer.toString((int)temp);在除以转化结束时
【解决方案3】:

这类东西有系统服务 HardwarePropertiesManager,其中包含方法 getDeviceTemperatures(int type, int source),可从 Nougat 获得

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    HardwarePropertiesManager hardwarePropertiesManager= (HardwarePropertiesManager) getSystemService(Context.HARDWARE_PROPERTIES_SERVICE);
     float[] temp = hardwarePropertiesManager.getDeviceTemperatures(HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU, HardwarePropertiesManager.TEMPERATURE_CURRENT);
}

看看这个: https://developer.android.com/reference/android/os/HardwarePropertiesManager

【讨论】:

  • 抛出这个异常
  • E/AndroidRuntime:致命异常:主进程:com.faircode.netguard,PID:17503 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.faircode.netguard/net.alimasood.cleanmaster .CpuCoolerActivity}:java.lang.SecurityException:调用者不是设备或配置文件所有者 r 绑定 VrListenerService。在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
  • 抱歉,我刚刚读到 HardwarePropertiesManager API 只允许在企业配置的 Android 设备中使用:(
  • java.lang.SecurityException: The caller is not a device owner.
  • "java.lang.SecurityException: 调用者既不是设备所有者,也不是持有 DEVICE_POWER 权限,也不是当前的 VrListener。" : "权限只授予系统应用"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
相关资源
最近更新 更多