Question

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.

Was it helpful?

Solution

Instead of a WakeLock, try getWindow().setFlags() using the WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON flag.

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.

OTHER TIPS

Here's how I did it...

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE, "DoNotDimScreen");

Followed by...

wl.acquire();

... when you need to actually activate the wake lock.

The 'PowerManager.ON_AFTER_RELEASE' flag pokes the user activity timer so the screen stays on for a little longer when this wake lock is released, which will be the case when your activity is Destroyed on a change of orientation.

Works OK for me!

I did it by actually never releasing the wakelock. Instead, I acquire it with timeout.

private PowerManager.WakeLock wl;

protected void onStart(Bundle savedInstanceState) {
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Words counter");
    // disable reference counting, as acquire/release calls are not paired
    wl.setReferenceCounted(false);
    wl.acquire();
}

protected void onStop() {
    // acquire for few seconds to keep lock until onStart is called with new orietnation
    wl.acquire(3000);
}

Advantages over other methods:

  • Usable with the SCREEN_DIM_WAKE_LOCK
  • Simple (no need to play with isFinishing() etc.)
  • Does not poke user activity after orientation change. I.e. dimmed display stays dimmed
  • If you release (e.g. the time is over but the user remained in the activity), it's released immediately. No need to wait for default timeout.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top