Monitor sensor events
To monitor raw sensor data, you need to implement two callback methods exposed through the SensorEventListener interface: onAccuracyChanged () and onSensorChanged (). The Android system calls these two methods when any of the following occurs:
1. Changes in sensor accuracy:
In this case, the system will call the onAccuracyChanged () method, which provides the Sensor object to be referenced that has a precision change. Precision is represented by one of the following four state constants:
SENSOR_STATUS_ACCURACY_LOW
SENSOR_STATUS_ACCURACY_MEDIUM
SENSOR_STATUS_ACCURACY_HIGH
SENSOR_STATUS_UNRELIABLE
2. The sensor reports new values:
In this case, the system will call the onSensorChanged () method, which provides a SensorEvent object. The SensorEvent object contains information about the new sensor data, including the data precision, the sensor that generates the data, the timestamp when the data is generated, and the new data recorded by the sensor.
The following code shows how to use the onSensorChanged () method to monitor data from the brightness sensor. In this example, the original sensor data is displayed in a TextView:
Publicclasssensinclutivityextendsactivityimplementssensoreventlistener {privateSensorManager mSensorManager; privateSensor mLight; @ Override publicfinalvoid onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mSensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); mLight = mSensorManager. getdefasensensor (Sensor. TYPE_LIGHT) ;}@ Override publicfinalvoid onAccuracyChanged (Sensor sensor, int accuracy) {// Do something here if sensor accuracy changes .} @ Override publicfinalvoid onSensorChanged (SensorEventevent) {// The light sensor returns a single value. // define sensors return 3 values, one for each axis. float lux = event. values [0]; // Do something with this sensor value .} @ Override
Protected void onResume (){
Super. onResume ();
MSensorManager. registerListener (this, mLight, SensorManager. SENSOR_DELAY_NORMAL );
}
@ Override
Protected void onPause (){
Super. onPause ();
MSensorManager. unregisterListener (this );
}
}
In this example, the default data delay (SENSOR_DELAY_NORMAL) is specified when the registerListener () method is called ). Data latency (or sampling rate) controls the interval at which sensor events are sent to your application using the onSensorChanged () callback method. The default data delay is used to monitor the changes in a typical screen direction, and the latency is 200,000 milliseconds. You can specify other data delay types, such as SENSOR_DELAY_GAME (20,000 Ms latency), SENSOR_DELAY_UI (60,000 Ms latency), or SENSOR_DELAY_FASTEST (0 Ms latency ). After Android3.0 (API Level 11), you can also use an absolute value (in milliseconds) to specify the latency.
The latency you specify is only the latency of the Creation. Android and other applications can modify this latency. As the best practice, you should specify the maximum latency you need, because the system usually uses a lower latency than you specify (that is, you should select the lowest sampling rate required by your application ). Large latency can reduce the workload of the processor and reduce the power consumption.
Is there a public way to determine the frequency at which the sensor framework sends sensor events to your applications? However, you can use the timestamps of the first and second sensor events to calculate the sampling rate. Once you set the sampling rate (latency), do not change. If you need to change the listener for some reason, you must log out and register the sensor listener again.
In this example, we will also focus on using the onResume () and onPause () callback methods to register and deregister sensor listening events. As the best practice, you should always disable the sensor when it is not needed, especially when the Activity is suspended. If this is not done, some sensors will soon consume the battery power because of their high power requirements. When the screen is off, the system does not automatically disable the sensor.