Android Advanced Sensor Usage
In the previous article, We learned about the basic properties of the SensorManager, SensorEventListner and Sensor objects along with their usage. In this article we will look closer at some of the more advanced sensor properties and processing sensor data.
Trigger Events
In addition to the previously discussed SensorEventListener, Android 4.3 introduced an additional mechanism for receiving sensor data: the TriggerEventListener. TriggerEventListener is mainly used for a special sensor type called significant motion. The signification motion sensor is a software sensor derived from hardware sensors present on the device. The only allowed value the sensor can return is 1.0. So what does this sensor actually do? It detects when the Android device has a sudden motion or jerk. Unlike SensorEventListener, the TriggerEventListener is a one shot: it is automatically canceled after the trigger. At the Java level, one major difference between the two listener types is that TriggerEventListener is a class not an interface, so we need to extend and implement the overridden method onTrigger(). The onTrigger() method receives a TriggerEvent object as a parameter. The TriggerEvent class is nearly the same as the SensorEvent class, except it doesn’t have onAccuracyChanged() method. This is because the trigger event is only fired a single time, after that it will unregister itself. Here’s a brief example of using TriggerEventListener:
public class TriggerListener extends TriggerEventListener {
private TextView mText;
public TriggerListener(TextView textView){
this.mText = textView;
}
@Override
public void onTrigger(TriggerEvent event) {
Log.i(“TriggerListener”, “Name:” + event.sensor.getName());
Log.i(“TriggerListener”,”Type:” + event.sensor.getType());
mText.setText(“Name: […]