Android Sleep/Standby Mode
Asked Answered
R

1

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.

Rake answered 25/2, 2011 at 16:58 Comment(0)
M
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.

Mola answered 25/2, 2011 at 17:2 Comment(16)
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?Rake
@Brandon: Yes. Since the CPU is powered down, all processes freeze in place.Mola
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.Tacita
@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.Mola
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?Tacita
@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.Mola
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?Tacita
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 developer.android.com/google/gcm/c2dm.htmlSalted
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.Amaryllis
@Amaryllis agreed!completely depends on how OEM is done the power management module.Republican
@Mola what about BT and BLE, does they run in sleep mode?Melinamelinda
@MarianPaździoch: Not that I am aware of, but I am not expert on Bluetooth or BLE.Mola
@Mola sleep mode does not appear to be defined on the Google docs. When you talk about sleep mode, do you mean Idle? Thanks.Slugabed
@the_prole: This answer is nearly seven years old. At the time, Doze mode did not exist.Mola
@Mola 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.Slugabed
@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".Mola

© 2022 - 2024 — McMap. All rights reserved.