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.
We created a
TemperatureActivity
and implemented it with theSensorEventListener
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 theisSensorPresent
Boolean variable. If the temperature sensor is not present, then we show the relevant message usingTextView
. The following code snippet shows the necessary steps:public class TemperatureActivity extends Activity implements SensorEventListener{ private SensorManager mSensorManager; private Sensor mSensor; private boolean isSensorPresent...