The procedure to get values from the phone's pressure sensor is exactly the same as the previous example showing getting values from temperature sensors. The only difference is the sensor type. To get values from the pressure sensor, we have to specify the sensor type as TYPE_PRESSURE
. All other best practices (initiating SensorManager
, the Sensor
object, and registering and unregistering the listener and sensor callback) remain the same as they were in the previous temperature sensor example.
Once we have atmospheric pressure from the phone's pressure sensor, we can calculate the altitude of the phone using the getAltitude(float p0, float p1)
method of the SensorManager
class. The first parameter of the altitude API is the atmospheric pressure at sea level, and the second parameter is the atmospheric pressure of the current location, which can be obtained from the phone...