public class SensorActivity extends Activity, implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mLight;
...
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
Always make sure to disable sensors you don't need, especially when your activity is paused. Failing to do so can drain the battery in just a few hours. Note that the system will not disable sensors automatically when the screen turns off.
onSensorChanged()
methodSensor data can change at a high rate, which means the system may call the onSensorChanged(SensorEvent)
method quite often. As a best practice, you should do as little as possible within the onSensorChanged(SensorEvent)
method so you don't block it.
Several methods and constants have been deprecated. In particular, the TYPE_ORIENTATION sensor type has been deprecated.
Always verify that a sensor exists on a device before you attempt to acquire data from it. Don't assume that a sensor exists simply because it's a frequently-used sensor.
Sensors can provide data at very high rates. Allowing the system to send extra data that you don't need wastes system resources and uses battery power.