Time for action – shake detection using the accelerometer sensor

One of the most common use cases of the accelerometer sensor is to detect the shaking of the phone. Shaking can act as a valuable input for the apps, especially when the phone screen is off. For example, a lot of music player apps allow you to change the songs just by shaking the phone. In our example, we will play a small audio MP3 file when the phone shake is detected using the accelerometer sensor:

  1. As the first step, we create the necessary infrastructure to get the values from the accelerometer sensor. We will create a ShakeDetectionActivity and follow the standard steps for getting values from a sensor. We will select the sensor type for TYPE_ACCELEROMETER in the getDefaultSensor() ...

Get Android Sensor Programming By Example 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.