Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_SERVICE);
    Sensor AmbientTemperatureSensor
            = mySensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
    if (AmbientTemperatureSensor != null) {
        mySensorManager.registerListener(
                AmbientTemperatureSensorListener,
                AmbientTemperatureSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    }

private final SensorEventListener AmbientTemperatureSensorListener = new 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));
        }
    }

};
like image 925
Zeeshan Mehdi Avatar asked Aug 21 '18 12:08

Zeeshan Mehdi


People also ask

How do I check my CPU temperature on my phone?

The app is called “CPU Monitor”. With CPU Monitor, you can view all the details pertaining to the processor in your Android device, including the CPU speed, CPU usage, and the CPU temperature. The data are processed in real-time and are also displayed in the notification/status bar (can be turned on under settings).

What is the normal CPU temperature Android?

But how can you tell if your phone is experiencing a problem? All phones have a normal temperature range of 37-43 degrees Celsius, or 98.6-109.4 degrees Fahrenheit.


2 Answers

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;
    }
}
like image 187
Zeeshan Mehdi Avatar answered Oct 06 '22 17:10

Zeeshan Mehdi


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.

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

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

Thermal Values

like image 23
Mrudul Tora Avatar answered Oct 06 '22 16:10

Mrudul Tora