Sensors

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 ...

Get Pro Android Apps Performance Optimization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.