Skip to content Skip to sidebar Skip to footer

How To Get Android Cpu Temperature Programmatically

Tried this but got 0.0 and on physical device nothing found.. Any way to get cpu temperature in android SensorManager mySensorManager = (SensorManager) getSystemService(SENSOR_SER

Solution 1:

publicstaticfloatcpuTemperature()
{
    Process process;
    try {
        process = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone0/temp");
        process.waitFor();
        BufferedReaderreader=newBufferedReader(newInputStreamReader(process.getInputStream()));
        Stringline= reader.readLine();
        if(line!=null) {
            floattemp= Float.parseFloat(line);
            return temp / 1000.0f;
        }else{
            return51.0f;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return0.0f;
    }
}

Solution 2:

You can find all the thermal values(temp and type) from this code (not only CPU temperature). And also remember that sys/class/thermal/thermal_zone0/temp not always point towards CPU temperature (in my case it was pointing towards battery temperature). Always use this code in background thread. I have tested it on real device as well as emulator and it was working fine.

publicvoidthermal() {
        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"); 
                }
            }
        }
    }

    publicStringthermalTemp(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 = newBufferedReader(newInputStreamReader(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;
                } elseif ((int) temp > 1000) {
                    temp = temp / 100;
                } elseif ((int) temp > 100) {
                    temp = temp / 10;
                }
            } else
                t = "0.0";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    publicStringthermalType(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 = newBufferedReader(newInputStreamReader(process.getInputStream()));
            line = reader.readLine();
            if (line != null) {
                type = line;
            }
            reader.close();
            process.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        returntype;
    }

Sample Output in Logcat (Image below is Real device output... On emulator it only showed the type battery and its temperature.) :

Thermal Values

Solution 3:

There is a system service for this kind of stuff HardwarePropertiesManager that contain a method getDeviceTemperatures(int type, int source) which is available from 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);
}

Take a look to this : https://developer.android.com/reference/android/os/HardwarePropertiesManager

Post a Comment for "How To Get Android Cpu Temperature Programmatically"