46

I have made an app that starts a service, which starts a timer, which fires off a small function after an amount of time. This is working great on the emulator and on the Motorola Droid 1, but it doesn't work on the Droid X when the phone has been put into sleep mode. What I have discovered is that the timer seems to pause when the phone is in sleep. It doesn't seem to do this on the Droid 1, or the emulator. I'm sure the workaround isn't too difficult, so I'm not asking for help(for once) I just want an explanation, to better understand this.

My question is what exactly does "sleep mode" do on android systems? What does it stop, what doesn't it stop, etc. By sleep mode I mean, of course, when you press the power button and the screen goes black. What exactly is happening? Any insight is appreciated.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Brandon
  • 1,373
  • 2
  • 12
  • 19

1 Answers1

66

I'm sure the workaround isn't too difficult

Using AlarmManager is a bit tricky.

My question is what exactly does "sleep mode" do on android systems?

Primarily, it shuts down the CPU. Along the way, non-essential radios (WiFi, GPS) will have been shut down as well.

What does it stop, what doesn't it stop, etc.

About the only thing you can count on not stopping is the GSM or CDMA radio (for incoming calls, SMSes, and IP packets) and AlarmManager.

By sleep mode I mean, of course, when you press the power button and the screen goes black.

Actually, that's not sleep mode, per se. That is the screen turning off.

The device will go into sleep mode shortly thereafter, if nothing is keeping it awake with a WakeLock. However, there is no guarantee that within a millisecond of you pressing that button and the screen turning off that the CPU is off.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    Somehow, I knew it would be you that answered this question. Ha. Thank you for this. So, it even stops Services when in sleep mode? – Brandon Feb 25 '11 at 17:08
  • 2
    @Brandon: Yes. Since the CPU is powered down, all processes freeze in place. – CommonsWare Feb 25 '11 at 19:06
  • 4
    is there any documentation (other than reading the actual source code) of when exactly the CPU would shut down, and when in general one should use a wake lock? especially for Services that need to send keep alives over the network. – Erdal Mar 17 '11 at 20:09
  • 3
    @Erdal: Android is not a RTOS -- there is no "exactly". If you need to be sending packets out on a socket, you need a WakeLock, plus a WifiLock if the connection is on WiFi. And, you want to run this for as few of milliseconds as is possible. – CommonsWare Mar 17 '11 at 20:19
  • that makes sense. I'm writing a chat application that has a Service running in the background and keeps a tcp socket connected all the time to the backend. It's OK for the phone to go to sleep while waiting for packets, but if I hold a wifi lock it would drain the battery life. On a 3G connection incoming packets awake the phone. Any suggestions on how this can be accomplished for wifi? – Erdal Mar 18 '11 at 04:27
  • 6
    @Erdal: IMHO, here is no reason for a "chat application" to have a running service except when the user is actively chatting, let alone have a service continue when the phone is off. And, given the loud chorus of complaints from users about developers that do this sort of thing, I suspect that at most you want this to be some sort of optional opt-in feature. That being said, what you want is impossible for WiFi except via a `WakeLock` and a `WifiLock`. For further discussion on this topic, open your own question, please. – CommonsWare Mar 18 '11 at 11:03
  • one more comment: how else could I receive messages (I don't want to use C2DM because I want to support pre 2.2) if I don't have a service running and a tcp connection connected all the time? – Erdal Mar 18 '11 at 18:58
  • I know it is a long time ago from last post/comment but to prevent somebody got stucked with C2DM when he should use GCM (supported trought Google-play-services) here is the link for migration http://developer.android.com/google/gcm/c2dm.html – Ewoks Nov 26 '13 at 13:44
  • 1
    At least on NEXUS 7 2013 (Android 4.4), CPU seems not to be shut down, even long after screen is dimmed and turned off, app and service (which is monitoring BLE devices) keep working just the same way it was in foreground. Maybe it depends on os version and phone manufacturer. – bob Dec 04 '13 at 23:26
  • @bob agreed!completely depends on how OEM is done the power management module. – NitZRobotKoder Mar 29 '14 at 00:56
  • @CommonsWare what about BT and BLE, does they run in sleep mode? – Marian Paździoch Mar 18 '15 at 09:59
  • @MarianPaździoch: Not that I am aware of, but I am not expert on Bluetooth or BLE. – CommonsWare Mar 18 '15 at 11:33
  • @CommonsWare `sleep mode` does not appear to be defined on the Google docs. When you talk about `sleep mode`, do you mean [Idle](https://developer.android.com/training/monitoring-device-state/doze-standby.html)? Thanks. – the_prole Jan 31 '18 at 19:39
  • @the_prole: This answer is nearly seven years old. At the time, Doze mode did not exist. – CommonsWare Jan 31 '18 at 19:40
  • @CommonsWare Wow, fair enough. I made a mistake in my language. I meant to ask you if the `sleep mode` you are talking about is the same as the `standby mode` in the doc I linked. – the_prole Jan 31 '18 at 19:45
  • @the_prole: At the time, I was referring to a state of the CPU. It is not the same as how Google defines "idle" or "standby mode". – CommonsWare Jan 31 '18 at 19:47