2

I have difficulty in understanding power management best practices and sleep mode state of android device.

  1. Sleep mode.

    1.1) when will this happen?

    1.2) CPU is going to sleep and my threads are suspend? and when will this happen?

    1.3) To prevent CPU sleeping I can use PARTIAL_WAKE wake lock or alarm manager? What the best for battery?

    1.4) How prevent shutdown of WiFi and 3G connection in sleep mode programmatically from my app?

P.S Yes, I understand that it is bad for battery lifetime! I try to find best compromise...

  1. I write my instant messenger application. I need have persistent connection to server. How the best way to achieve this? After ~20-30 minute with screen off I lost internet connection but Skype stays online. How they do it?

  2. I know about C2DM framework. But it is unidirectional from server to application. How the best way to use it for me? Server must know that client really connected!

Dmitry
  • 369
  • 4
  • 18

1 Answers1

5

when will this happen?

Sometime after the screen turns off, based on user settings for inactivity.

CPU is going to sleep and my threads are suspend?

Yes.

when will this happen?

You asked that already.

To prevent CPU sleeping I can use PARTIAL_WAKE wake lock or alarm manager?

AlarmManager can cause the phone to wake up out of sleep mode, but only very briefly. A WakeLock prevents the device from going into sleep mode in the first place.

How prevent shutdown of WiFi and 3G connection in sleep mode programmatically from my app?

Use a WifiLock to prevent the WiFi radio from shutting down. The telephony radio is unaffected by sleep mode.

How the best way to achieve this?

If the user is not actively using the device, allow the connection to terminate. By definition, they are not participating in the IM session if their device falls asleep.

I know about C2DM framework

This has been replaced by GCM, which has the same basic characteristics.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you CommonsWare. "By definition, they are not participating in the IM session if their device falls asleep." But why Skype show online status? – Dmitry Feb 19 '13 at 13:34
  • @Quadri: You would have to ask Microsoft. In Skype's case, they are not really an IM client, IMHO. – CommonsWare Feb 19 '13 at 13:39
  • I would not agree with @CommonsWare on the second last question. Google hangout can be online 24*7 and GCM push framework. If you want, you can send message to device in real time, no delay. GCM client maintains a background service serving a live connection to Google GCM server. On TCP layer, the connection is persistent, afaik, it is live. – lucas May 04 '14 at 08:47
  • , which means, if there is no data coming from the server side, the base station can terminate RRC layer connection, thus saving device battery. If there are data from the server, the base station resumes the connection to device, and the device enters radio full power mode to communicate with the server. Device sleep does not mean it cannot receive data from the server completely. – lucas May 04 '14 at 08:54
  • "Sometime after the screen turns off", can you give a vague idea of how long that time is? that is, how long does it take for the system to put CPU to sleep after the screen is turned off. In my experience, threads run fine even after the screen is turned off, for minutes at least – ssynhtn Jun 20 '21 at 18:45
  • @ssynhtn: "can you give a vague idea of how long that time is?" -- there are tons of variables: the amount of system RAM in the device, what other apps are installed and running, the device manufacturer, the version of Android, and so on. https://dontkillmyapp.com/ will give you a general sense of the overall situation regarding background work. – CommonsWare Jun 20 '21 at 23:10
  • @CommonsWare hi, thanks for the reply. I don't particularly care about manufacture ROMs, for this question, I only want to know how stock works, do you know if there is something like doze mode's 30 minutes rule(that is deep doze mode starts when some criteria is satisfied for 30 minutes), if not, how long in general is the duration from screen off to CPU off, is it like in the range of 3~10 minutes or around half an hour? – ssynhtn Jun 21 '21 at 02:22
  • @ssynhtn: I have no way of answering that, sorry. – CommonsWare Jun 21 '21 at 11:02