1. Android Sensors
Sensors Android supports several sensors via the
SensorManager
, for example the accelerometer. Unfortunately you cannot test the accelerometer on the Android emulator.
You can access a
SensorManager
via getSystemService(SENSOR_SERVICE)
. The Sensor
class defines several constants for accessing the different sensors.- Sensor.TYPE_GYROSCOPE
- Sensor.TYPE_MAGNETIC_FIELD
- Sensor.TYPE_ORIENTATION
- Sensor.TYPE_ACCELEROMETER
You can access the Sensor via the
sensorManager.getDefaultSensor()
method, which take as parameters the sensor type and the deplay defined as constants on SensorManager
.
Once you acquired a sensor, you can register a
SensorEventListener
object on it. This listener will get informed, if the sensor data changes.
To avoid the unnecessary usage of battery you register your listener in the
onResume()
method and de-register it in the onPause()
method.
No comments:
Post a Comment