What is the definition of asleep for an android device?

前端 未结 2 1983
忘掉有多难
忘掉有多难 2021-01-04 14:02

I\'m using

alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, DURATION, broadcast);

To schedule an repeating task that sho

2条回答
  •  佛祖请我去吃肉
    2021-01-04 14:22

    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.

提交回复
热议问题