What is the definition of asleep for an android device?

为君一笑 提交于 2019-11-30 18:34:53

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!