January 2012
Intermediate to advanced
282 pages
7h 4m
English
Sensors are fun. Everybody likes them and everybody wants to use them. The way to use sensors is very similar to the way to use location providers: your application registers a sensor listener with a specific sensor and is notified of updates. Listing 7–15 shows how you can register a listener with the device’s accelerometer.
Listing 7–15. Registering Sensor Listener With Accelerometer
private void registerWithAccelerometer() {
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (sensors != null&& ! sensors.isEmpty()) {
SensorEventListener listener = new SensorEventListener() {
@Override ...