views:

541

answers:

1

I'm using Wakelock in my application to prevent the phone from sleeping when the app is visible.

The problem comes when there is an orientation change and the system destroys and re-creates the app in landscape mode. If the wakelock timer has expired in the background the system takes the release of wakelock as an opportunity to switch off the screen.

Edit: I should mention that I am setting Wakelock onResume, and releasing onPause - as I understand it, it's necessary to release then to prevent the app from leaking the wakelock.

I need wakelock to continue through the orientation change.

Below I've outlined a solution to this. Is this the preferred way to handle it, and are there any alternatives?


Create a service which holds wakelock (rather than the activity) and when the activity unbinds the service starts a countdown timer (for say 10 seconds) in which it will release wakelock when the timer expires if the activity does not rebind. If it was a simple orientation change the activity will rebind within that 10 seconds and so maintain wakelock, if it doesn't, wakelock will be released.

Thanks.

+1  A: 

Instead of a WakeLock, try getWindow().setFlags(WindowManager.LayoutParams.KEEP_SCREEN_ON).

If the wakelock timer has expired in the background the system takes the release of wakelock as an opportunity to switch off the screen.

It shouldn't. By definition, the user has interacted with the device, so the screen should stay on for that reason, independent of anything else.

CommonsWare
Thanks for your response. I will look into trying your suggestion. -- Wakelock is working as expected in all aspects apart from the one you've quoted here. Any ideas why this might be occurring?
bdls
Setting the KEEP_SCREEN_ON flag was a better solution, and solved my orientation change problem. Thanks.
bdls