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










1















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?










share|improve this question






















  • 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















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?










share|improve this question






















  • 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












1








1








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?










share|improve this question














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?







android android-service wakelock android-wake-lock foreground-service






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 22:13









Florian WaltherFlorian Walther

9971524




9971524












  • 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


















  • 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

















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






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 Answer
1






active

oldest

votes


















0














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.






share|improve this answer























  • 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 9: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 2: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 8:41










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290305%2fdoes-a-foreground-service-need-a-wake-lockt-to-stay-active-after-screen-off%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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.






share|improve this answer























  • 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 9: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 2: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 8:41















0














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.






share|improve this answer























  • 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 9: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 2: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 8:41













0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 2:00









Dewey ReedDewey Reed

9831514




9831514












  • 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 9: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 2: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 8: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 9: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 2: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 8: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 9:10





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 9: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





@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





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 2:23






@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 2: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 8:41





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 8:41

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290305%2fdoes-a-foreground-service-need-a-wake-lockt-to-stay-active-after-screen-off%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto