Wakelock and Doze Mode

Wakelock and doze mode

Based on some testing, using a Nexus 5 with the the final(?) preview of Android 6.0 installed:

  • Holding a PARTIAL_WAKE_LOCK is insufficient to block Doze mode — the device will still doze, even though you have the WakeLock and are trying to do regular work (e.g., setExactAndAllowWhileIdle() to get control every minute)

  • Keeping the screen on using android:keepScreenOn (or the Java equivalent), with the screen on, is sufficient to block Doze mode

  • Keeping the screen on using android:keepScreenOn (or the Java equivalent), with the screen off (user presses POWER button), is insufficient to block Doze mode

IOW, video players and the like should not be affected while the user is watching the video, even though the player may not be moving or charging. However, if the user presses the POWER button, you're back into having Doze risk.

I have not tried using FULL_WAKE_LOCK (I would expect behavior identical to android:keepScreenOn, but I am far from certain).

setExactAndAllowWhileIdle with Wakelock in Doze mode

Yes, Doze will ignore your wakelock. However with setExactAndAllowWhile Idle you will be worken up at the correct time, and you'll have that 10s window to do any processing you wish.

What exactly do wake-locks prevent?

what exactly is meant by "keep the CPU running in order to complete some work before the device goes to sleep"?

Android devices can power down their CPUs to reduce battery consumption. This usually happens shortly after the screen turns off.

A partial wakelock says "allow the screen to turn off but keep the CPU powered on". This is used for things like long-running audio playback (music, audiobooks, podcasts, etc.).

A full wakelock says "do not allow the screen to turn off either". This is used for things like video players, where the user's expectation is that the screen will stay on despite limited user input.

Android doze mode and foreground service

According to this SO question and answer:

In this post's comments on Sep 17 Dianne Hackborn writes:

Apps that have been running foreground services (with the associated notification) are not restricted by doze.

- Source (sec_aw)

It looks like using a foreground service prevents the app from being killed by Doze.

Please note though, that some vendors (I know of Samsung, but there are probably others) create their own RAM conservation/battery saving tools. These may exhibit patterns completely different from Doze, and there's usually a bunch of other hoops to jump through. You are not guaranteed to get the same behavior on these devices either - they may be more or less aggressive, and the more aggressive ones tend to be worse for keeping services or whatever alive.

There's also no universal way to deal with these, but someone has made an entire website dedicated to showing what vendors are problematic, as well as potential workarounds. Note that the majority of the problematic vendors only yield end-user solutions, which means you (the developer) have no way to fix it without the user doing something.

While this is unfortunate, that's pretty much what happens when companies implement their own versions of Android. Also, from my own experience (primarily as a user rather than a dev) on a Samsung phone, the settings aren't always respected and still causes annoying behavior.

Anyway, as long as Doze is present, foreground services should be fine. On other vendors, however, all bets are off and you're at the mercy of the vendor's implementation of some type of optimization system. On certain exposed operating systems (again, see the website linked earlier), you also have no choice but to ask the user to fix certain settings to keep stuff alive.

Keep ForegroundService alive When in Doze Mode

As per this, the following is a restriction which applies in doze mode :

The system ignores wake locks.

You still need to hold a wakelock "to indicate that your application needs to have the device stay on".

So, your wake lock will not affect doze mode (and that is by design). However, if you are using a foreground service, doze mode does not seem to affect foreground services see this SO post.

If you are using something else, you can see this guide here to optimize your app for doze mode.



Related Topics



Leave a reply



Submit