Time for action – using the temperature sensor

In this section, we will get the ambient temperature from the phone temperature sensor and will show it in the foreground activity. Before accessing the value for ambient temperature, we will also check the availability of the temperature sensor on the device.

  1. We created a TemperatureActivity and implemented it with the SensorEventListener interface so that it receives the ambient temperature values. We are also checking if the ambient temperature is available on the device and are maintaining the availability state using the isSensorPresent Boolean variable. If the temperature sensor is not present, then we show the relevant message using  TextView. The following code snippet shows the necessary steps: ...

Get Android Sensor Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.