Some sensors do not provide a continuous stream of data, but rather raise an event when something happens.
To use trigger-based sensors, we also make use of the sensor manager. Unlike stream-based sensors, we inherit from the TriggerEventListener
type:
First, we need the instance of the
SensorManager
type:var manager = SensorManager.FromContext(this);
Next, we get a reference to the sensor. If the sensor is
null
, then it is not available on the device:var type = SensorType.SignificantMotion; var motion = manager.GetDefaultSensor(type); if (motion == null) { // handle no significant motion sensor }
Then, we create inherit from the
TriggerEventListener
type:private class MyListener : TriggerEventListener { public override void OnTrigger(TriggerEvent e) { // handle significant motion } }
We create an instance of this trigger event listener:
listener = new MyListener();
Once we have the listener, we register it with the sensor manager along with...