Shashank Srivastava
Shashank Srivastava

Reputation: 446

Getting CPU Temperature from the android device

I am trying to get the temperature of device . I have used Environment Sensor (Ambient Temperature) but this is not working on most of the devices. I also used commands like " sys/class/thermal/thermal_zone0/temp" but no luck.

Please let me know if anyone able to access the temperature on the latest devices like (Samsung S10), Vivi 1938

Upvotes: 1

Views: 357

Answers (1)

Dev4Life
Dev4Life

Reputation: 3307

You can get the device's CPU temperature using this code:

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;
 }
}

Upvotes: 0

Related Questions