java Example
public class SensorActivity extends Activity, implements SensorEventListener { private final SensorManager mSensorManager; private final Sensor mAccelerometer; public SensorActivity() { mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); } protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { } }
See Also: SensorManager Members
java Example
public class SensorActivity extends Activity, implements SensorEventListener { private final SensorManager mSensorManager; private final Sensor mAccelerometer; public SensorActivity() { mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); } protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { } }
SensorManager lets you access the device's Android.Hardware.Sensor. Get an instance of this class by calling Android.Content.Context.GetSystemService(string) with the argument Android.Content.Context.SensorService.
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.
Note: Don't use this mechanism with a Trigger Sensor, have a look at Android.Hardware.TriggerEventListener. Sensor.TYPE_SIGNIFICANT_MOTION is an example of a trigger sensor.