There are a wide and growing variety of sensors that can be found on an Android handset, from accelerometers and gyroscopes to light and proximity sensors. Most of these devices can be accessed with the android.hardware.SensorEvent class, although naturally they each produce their own specific data sets.
Here we will use Sensor.TYPE_ACCELEROMETER
to measure a handset's motion in three dimensions before going on to explore other sensor types.
Gathering information from sensors is quite straightforward as Android provides a handy interface, android.hardware.SensorEventListener, to facilitate this. Nevertheless there is a little more housekeeping required than previous tasks as we must take control of the registering of these listeners with the SensorManager class.
Start up a new Android project in Eclipse and create a TextView with an ID in main.xml
.