3

Im Developing a Service for android marshmallow which intend to keep running for a long period of time. My questions are"

What will happen to my service when the device goes into deep sleep?

Is there any way to control what happens when the system pauses the service to go to sleep ( like the onPause () method for activities)?

I have searched for an answer on Google unsuccessfully but if my question has already been answered I'm really sorry for wasting your time and would appreciate if you would point me towards the correct answer.

Thanks

Floern
  • 33,559
  • 24
  • 104
  • 119

1 Answers1

2

What will happen to my service when the device goes into deep sleep?

If your service is running, the device will not go into deep sleep. Deep sleep only happens if the CPU has literally nothing to do, which is not the case if your service is doing stuff.

Is there any way to control what happens when the system pauses the service to go to sleep

I don't fully understand that question. A service does not go to sleep. It is either running, or it's not. You control the lifecycle (apart from the OS killing the service's process when it needs to free up memory).

Community
  • 1
  • 1
Tim
  • 41,901
  • 18
  • 127
  • 145
  • Thank you for the answer. However I was under the impression that(correct me if I'm wrong) unless a wakelock has been engaged by an app or service the system WILL go to sleep. Proof of that would be Google services which is running most of the time. Also from what I have read over the internet programmers often complain that their service malfunctions when the device sleeps in which case, said programmers are advised to use a wake lock. What I'm curious about is what exactly happens. Does the system kill the service or pause its thread? – Talha Siddiqi Oct 26 '16 at 11:48
  • A wake lock will prevent the CPU from sleeping, but there are different types of sleep. You can use a wakelock, but make sure that you only hold it for as long as you need it, so when you're done, release it and acquire it again when you need it again – Tim Oct 26 '16 at 12:10
  • So what you're saying is that the system never goes into deep sleep because the Google play service is running all the time? If that's the case, what is the purpose of deep sleep? – Talha Siddiqi Oct 26 '16 at 12:13
  • A service can run without doing actual work. You can start a service and not let it do any work. It will be running, but it won't be doing anything. Maybe "running" is not the correct terminology but I hope you understand what I mean – Tim Oct 26 '16 at 12:23
  • Yes I get what you mean ( e.g. if a service is listening for notifications). So will that app prevent deep sleep? – Talha Siddiqi Oct 26 '16 at 12:24
  • If it is *polling* for notifications, yes (=actively doing work). If it's **waiting** for an *event*, no – Tim Oct 26 '16 at 12:26
  • So if its waiting for an event and system goes to deep sleep, what happens to the service? – Talha Siddiqi Oct 26 '16 at 12:28
  • It will not receive the event at that time. The CPU will wake itself after some time and resume operations then. I don't know if the event will be received at the time the CPU wakes back up – Tim Oct 26 '16 at 12:40
  • So that means that the service IS paused – Talha Siddiqi Oct 26 '16 at 12:42
  • You're free to interpret it how you want – Tim Oct 26 '16 at 12:44