What is the definition of asleep for an android device?
I'm using
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, DURATION, broadcast);
To schedule an repeating task that should only be executed if the device is not asleep. As I understand the API on the AlarmManager the intent will be called once the device wakes up from sleep.
Now when is an Android device asleep? For testing I set the duration to two minutes and connected the de开发者_Go百科vice to my machine. Now I'm watching logcat and every two minutes it will spit out my debug message.
I always thought that an deactivated screen means that the devices starts sleeping. Or is my looking at the debug output preventing the device from sleeping? I also disconnected the USB connection and looked at the log after half an hour and I could see a call to my timer code even if the display was dark for more then 15 minutes.
How can I verify from which time the command is not executed anymore and what asleep refers to in the AlarmManager documentation? Is there a way to see from the logcat output when the device started sleeping?
A screen that is off, does not necessarily indicate a sleeping device. Applications can request wake locks that will keep the device awake while the screen is off. If another app has a timer that does not use inexact repeating, then the device will wake up for that as well.
Depending on the version of Android on your device, you can find a chart of your awake time in Settings - > About Phone -> Battery Use -> Then clicking on the graph up top. More information on wake locks can be found at http://developer.android.com/reference/android/os/PowerManager.html
Device is asleep when there is not running application that prevents it from sleeping. So: 1. The screen is off (while it's on there is always some running app, e.g.Launcher) 2. There is no running service (e.g. music, downloads) - no CPU locks.
AlarmManager wakes device from sleeping, runs what is to run. It's critical e.g. to ends a service that was created in broadcast receiver from alarm to let the device fall asleep again. If you do sth longer and important in the background you should acquire CPU lock for your app to prevent it from being killed by Android OS.
What do you exactly mean by "How can I verify from which time the command is not executed anymore"? What command?
From JavaDoc:
Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
So OS hold all alarms, device can be waken up by the alarm that goes while it is sleeping. Alarms are dropped after reboot (you should keep them in DB and restore them after reboot).
"Is there a way to see from the logcat output when the device started sleeping?" As I know there isn't, I can only see sth like that when screen goes off:
*** set_screen_state 0
from "power" tag
IMHO you shouldn't bother about sleep mode, just make your app "good Android citizen" by handling screen off and on intents (Intent.ACTION_SCREEN_OFF) if needed, finishing services ASAP, releasing resources (network, GPS, CPU) ASAP, using inexact alarms, download manager, and all good staff brought by OS. I believe that Android handles CPU sleeps transparently.
精彩评论