Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a foreground service need a wake lockt to stay active after screen-off?

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?

like image 570
Florian Walther Avatar asked Oct 13 '25 05:10

Florian Walther


1 Answers

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.

like image 186
Dewey Reed Avatar answered Oct 15 '25 09:10

Dewey Reed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!