I'm working on an Android app that turns a device's screen off when the light sensor value is below a threshold value and turns the screen on again when the light sensor value rises above the threshold value. This threshold value is decided by the user through a one-time calibration. Everything works great, the screen turns on and off when it's supposed to and I've dealt with the constant fluctuations in the light sensor's values so that the screen doesn't keep turning on and off randomly.
The issue I'm facing is that when the power button is pressed, the light sensor's "onSensorChanged" event is triggered for some reason. This means that if the user presses the power button to turn the screen off, it's automatically turn back on if it's in the right lighting.
The solution I thought of was to keep a flag variable that won't allow the code in my service to execute if the power button was pressed but the issue there is that Android does not allow me to detect a power button key press from a service.
Is there another possible (perhaps a more logical) workaround to this issue?
I don't think it would be helpful to post the code but let me know if you need me to post it.
Thanks.