3

In the wake-lock training doc it says:

If you need to keep the CPU running in order to complete some work before the device goes to sleep, you can use a PowerManager system service feature called wake locks.

It was my impression that "before the device goes to sleep" referred to doze mode. However, the answer to the SO post Wakelock and doze mode states:

Holding a PARTIAL_WAKE_LOCK is insufficient to block Doze mode

So, if a wake-lock doesn't prevent doze mode, then what exactly is meant by "keep the CPU running in order to complete some work before the device goes to sleep"?

Also, in the doze standby training doc it says:

An app that is partially exempt can use the network and hold partial wake locks during Doze and App Standby.

If (for some reason) "before the devices goes to sleep" does refer to doze mode, then does this mean that wake-locks have absolutely no effect unless you are on the white list for no battery optimizations?

Specifically, I'm talking about partial wake-locks on API 31+.

  • Have you read: https://developer.android.com/topic/performance/vitals/wakelock Also be aware that the poor battery life of early Android device (with apps using wake locks) means that OEMs have battery management outside of the Android framework: https://dontkillmyapp.com/ – Morrison Chang Jul 03 '22 at 06:29
  • @MorrisonChang Yes and the article is irrelevant to the question. Its about "Stuck partial wake locks" and it does not clarify by what it means for a wake-lock to prevent the device from sleeping (it doesn't even say the word "sleep" once). –  Jul 03 '22 at 06:35
  • From: https://developer.android.com/reference/android/os/PowerManager#FULL_WAKE_LOCK `Wake lock level: Ensures that the screen and keyboard backlight are on at full brightness.` and https://developer.android.com/reference/android/os/PowerManager#PARTIAL_WAKE_LOCK `Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off.` just to level set what Android calls a 'wake-lock'. Its more a name for level of power usage and prior to Doze mode apps could run continuously in the background, with resultant battery drain, i.e. hold access to CPU. – Morrison Chang Jul 03 '22 at 06:50
  • @MorrisonChang I think you're misunderstanding my question. I *know* what the documentation says about wake-locks. I'm confused by what the documentation means by the word "sleep". Specifically, I am wondering if "sleep" refers to doze mode. If it does, then why (contrary to what the docs say) is holding a wake-lock insufficient to stop doze mode? –  Jul 03 '22 at 06:57
  • Docs were written at different times. "sleep" or "idle mode" early in Android history meant that the CPU could power down as no app was requesting resources. A wake lock would prevent the CPU from sleeping as it was needed for work. Doze mode and the other battery related stuff happened more recently and tries to make clear to legacy developers that power usage is more restrictive. Understandable for a new dev to be confused as the wording is targeted at old devs, i.e. you can't hold the CPU forever. – Morrison Chang Jul 03 '22 at 07:12

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Does this mean that "sleep" does not refer to doze mode? Can the CPU effectively "sleep" outside of doze mode? And there seems to be a mixup in what I am asking about; I understand the differences between the different types of wake locks. I just don't know what "sleep" specifically refers to. –  Jul 03 '22 at 19:23
  • 1
    @free_coupons_for_sale_1023: "Does this mean that "sleep" does not refer to doze mode?" -- correct. "Can the CPU effectively "sleep" outside of doze mode?" -- yes. Doze mode was introduced a better part of a decade after wakelocks. And wakelocks are now part of the Linux kernel, which does not have Android's doze mode. "I understand the differences between the different types of wake locks" -- however, this answer is not solely for your benefit. It is also for the benefit of the thousands of developers who will read it over the years. – CommonsWare Jul 03 '22 at 19:35