Engaging Sensors

The Android SDK supports many different types of sensor devices:

• TYPE_ACCELEROMETER: Measures acceleration in the x-, y-, and z-axes

• TYPE_LIGHT: Tells you how bright your surrounding area is

• TYPE_MAGNETIC_FIELD: Returns magnetic attraction in the x-, y-, and z-axes

• TYPE_ORIENTATION: Measures the yaw, pitch, and roll of the device

• TYPE_PRESSURE: Senses the current atmospheric pressure

• TYPE_PROXIMITY: Provides the distance between the sensor and some object

• TYPE_TEMPERATURE: Measures the temperature of the surrounding area

Not all devices will offer all this functionality, of course.4

The SensorTest example, available on the book's website, demonstrates using the Sensor API. Android's SensorManager class is similar to Loca-tionManager, except the updates will come much more quickly, perhaps hundreds per second. To get access to the sensors, you first call the getSystemService( ) method like this:

Download SensorTest/src/org/example/sensortest/SensorTest.java

mgr = (SensorManager) getSystemService(SENSOR_SERVICE);

Then you call the registerListener() in your onResume() method to start getting updates and call unregisterListener( ) in your onPause( ) method to stop getting them.

0 0

Post a comment