Last active
September 24, 2023 12:01
-
-
Save malihehmoradi/21747568e64bd5121c0b849976af98e5 to your computer and use it in GitHub Desktop.
Get last ambient temperature from android device with temperature sensor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SensorActivity extends Activity implements SensorEventListener { | |
private final SensorManager mSensorManager; | |
private final Sensor mTempSensor; | |
private float lastTemperature= 0F | |
public SensorActivity() { | |
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) { | |
lastTemperature=event.values[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment