问题
I have difficulty in understanding power management best practices and sleep mode state of android device.
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...
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?
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!
回答1:
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.
来源:https://stackoverflow.com/questions/14957645/android-power-management-sleep-mode-etc