I want to get temperature from device. but I don t know how I can do. please help me writing simple. thanks.
how to get device 's temperature in android [closed]
Asked Answered
You can use TYPE_AMBIENT_TEMPERATURE for battery or CPU temperature. TYPE_TEMPERATURE is the depcrecated constant.
The modified version of code available at documentation should be like this:
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class TempSensorActivity extends Activity, implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mTempSensor;
public TempSensorActivity() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mTempSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mTempSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
}
}
Look at sensor documentation for more.
This is very helpful but there's a more complete answer here: https://mcmap.net/q/833181/-how-to-measure-ambient-temperature-in-android –
Henderson
Why do system return null for mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE) on every device that I test? Other apps display the temperature on the same device. How it is possible? For example, I use Samsung S9 and system returns null too. –
Ronaldronalda
Same null problem as @PonomarenkoOleh ... any one solved this issue? –
Templet
© 2022 - 2024 — McMap. All rights reserved.