Search code examples
androidproximity

How to prevent proximity sensor from sleeping when the screen is off?


I am trying to make an application to switch on the screen when the hand is placed close to proximity sensor. However, the proximity sensor shuts down as soon as the screen is switched off. How do I prevent this?


Solution

  • You should write a service that registers for sensor input and uses onSensorChanged() . This way you can still listen to the event when the screen is turned off and the activity is paused.

    However in Marshmallow and Nougat you have doze mode, which means the service will most probably stop running after the screen is off for a long time. I think they didn't specify behaviour for sensor change in their documentation, I'd love to hear what happens.