4

In different sources, I read that a foreground service requires a wake lock to stay active after the device goes to sleep. But when I test it by starting a foreground service and turning the screen off while the device is unplugged (both on emulator and on a real Samsung device), the foreground service keeps running.

Does a foreground service require a (partial) wake lock to stay active after the screen is off?

Florian Walther
  • 6,237
  • 5
  • 46
  • 104
  • Did you get any more info on this? I see the same on my Motorola Moto E4 plus. It just keeps running, so I am wondering what is the purpose of WakeLock. Even if you need it, it seems strange that the CPU needs to stay awake all the time to execute small pieces of code periodically. – kodu Nov 28 '18 at 10:16

1 Answers1

4

From my experience of developing a timer, the answer is yes, especially when the screen is off.

Without a wake lock, the foreground service will be killed or suspended in a few minutes(2 ~ 10m in my tests). Sometimes, when the screen is off, the code won't be executed but the foreground notification still exists and the code only starts being executed after the screen is turned on. This makes debug very hard. This situation is more common if the test device is from Chinese manufacturers(Foreground service + Wake Lock + Letting user whitelist your app seems the only solid option if your app targets Chinese market).

Use a wake lock if you want your service keep running after the screen is off.

Dewey Reed
  • 4,353
  • 2
  • 27
  • 41
  • That's weird because I have tested it on emulator and my Galaxy Note 8 and on both devices the service was never killed without a wake lock. I actually started a long-running foreground service and kept it running overnight with the screen off and no charger plugged in and it was still running this morning. – Florian Walther Nov 14 '18 at 09:10
  • @FlorianWalther I prefer not to trust emulator test result. FYI, I checked a music player app before and it has both foreground service and a wake lock. – Dewey Reed Nov 14 '18 at 12:54
  • Can you show me that music player? Also, I tested it as a foreground service without a wake lock on my real device and it ran through the whole night. I just wish I would find a situation where the service was actually killed. From my tests, only jobs get deferred. – Florian Walther Nov 14 '18 at 13:32
  • @FlorianWalther I'm sorry but I did that test long time ago and have already forgot the music player's name. You may want to turn on idle or app standby or something through adb to test your app. but why is jobs getting deferred not a problem? – Dewey Reed Nov 15 '18 at 02:23
  • Well I try to get the hang of the wake lock part, but right now everything works the same for me with and without wake locks. – Florian Walther Nov 15 '18 at 08:41
  • @FlorianWalther I'm facing the exact problem statted by DeweyReed I have developed a Multi-Timer App. The App is working perfectly in all devices except the Chinese OS (Huawei). It is true Foreground Service overrides the Android Doze mode but in Chinese OS devices, I don't know why they modified so much of android ASOP. – Muhammad Farhan Jan 13 '20 at 08:06
  • In my case foreground service works fine but system stop network access after 10 minutes of screen off. Pixels are working fine but Samsung and other Chinese manufacturers are not following the rules. Only option to add the app in ignore battery optimization list – shantanu Oct 26 '22 at 09:58
  • @shantanu On some Chinese devices even "ignore battery optimisation" is not helping me keep network access to my app. – Mohsin Falak Nov 28 '22 at 08:55
  • @MohsinFalak You need more than it. Check this: https://dontkillmyapp.com/ – Dewey Reed Nov 29 '22 at 02:20