Search code examples
androidwakelockandroid-wake-lock

Do you need a WakeLock for CPU intensive services?


It is not really clear to me from the documentation if a WakeLock should be used for my applications. In all cases these services are started with startForeground():

  1. Process audio and play on an AudioTrack. (CPU intensive).
  2. Process audio and encode to a file. (CPU intensive).
  3. Receive periodic location updates from Google Play Services (5-10 seconds apart, probably less CPU intensive).

I have tested the first two on my devices (stock Android 4.4 and 5.0) for > 15 minutes (likely longer than they will ever need to stay running), and the services continue running with the screen off and no WakeLock. The third one I have not yet tested. I realize devices can have different characteristics.

Should I be using a WakeLock for these services, or does the CPU activity keep the device from sleeping? They are not supposed to stop when the device isn't being used.


Solution

  • Should I be using a WakeLock for these services

    If you don't want the device to fall asleep, and nothing else is keeping the device awake, yes.

    does the CPU activity keep the device from sleeping?

    No.

    the services continue running with the screen off and no WakeLock

    Then either:

    • your devices are plugged into power (and you have the associated developer setting to keep the CPU on while plugged in enabled), or

    • something else is holding a WakeLock, or

    • your hardware is specialized to not put the device in sleep mode, or

    • you are misinterpreting your test results