Meta

Spam Blocked

Using proximity sensor in Android App

Proximity Sensor in Android device is really helpful while building interactive applications, it can help you to add very good usability features in application.

For more technical details of proximity sensor and how it works check Wikipedia.

The main function of proximity sensor is to detect presence of nearby objects without any physical contact.

Now in many interactive apps it can be used like :

– Receive a call when user picks up the device and takes it near their ear
– Automate audio recording when user takes device near their ear
– Reject call when phone is placed upside down
– Turnoff screen when phone is in pocket
– Turnoff screen when phone is near ear to avoid accidental touches

We used this feature for our app to start playing a sound through earpiece when user takes device near their ear. It took some time to get it working so we thought to share the code so it can be helpful for others, you can use below code to get it working easily.