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 device 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?
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:
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 0from “power” tagIMHO 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.