Android sensor

1. Android Sensors

1.1. SensorManager

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.

1.2. Sensor listener

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top