2

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.

4

1 回答 1

0

Instead of tracking a flag for the power button press, why not keep a flag for when you turn it off? That way if it was turned off for any other reason, it won't do anything.

Edit: Another idea...

What you could do is track the last time the ACTION_SCREEN_OFF intent was fired. If the time is less than a certain threshold(0.05s?) from when your onSensorChanged is called, you can just disregard(or at least not turn it back on).

于 2013-07-25T17:53:19.710 回答