Discovering the environment
Not all sensors are used to access the state of the device. Some are used to describe the world in which the device exists.
How to do it...
The environment sensors are the same as most of the sensors, in that they require an implementation of the sensor event listener interface to be registered with the sensor manager:
- As with all sensors, we have to use the
SensorManager
instance to access the sensor:var manager = SensorManager.FromContext(this);
- When we have the sensor manager, we obtain the specific sensor:
var type = SensorType.Light; var light = manager.GetDefaultSensor(type); if (light == null) { // handle no significant motion sensor }
- Next, we implement the
ISensorEventListener
interface:public class MyActivity : Activity, ISensorEventListener { public void OnAccuracyChanged( Sensor sensor, SensorStatus accuracy) { } public void OnSensorChanged(SensorEvent e) { } }
- Then, we implement the
OnSensorChanged()
method to read the single value from the...