Android 设备睡眠的定义是什么?
我正在使用
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, DURATION, broadcast);
安排一个重复任务,该任务仅在设备未睡眠时才执行。据我了解 AlarmManager 上的 API 意图将设备从睡眠状态唤醒后被调用。
Android 设备什么时候处于睡眠状态?为了进行测试,我将持续时间设置为两分钟并将设备连接到我的机器。现在我正在观看 logcat,每两分钟它就会吐出我的调试消息。
我一直认为停用的屏幕意味着设备开始休眠。 或者我查看调试输出是否会阻止设备休眠? 我还断开了 USB 连接,并在半小时后查看日志,即使显示屏黑暗超过 15 分钟,我也可以看到对我的计时器代码的调用。
如何验证从哪个时间开始不再执行该命令以及 AlarmManager 文档中 sleep 指的是什么?有没有办法从 logcat 输出中查看设备何时开始睡眠?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
屏幕关闭并不一定表示设备处于睡眠状态。应用程序可以请求唤醒锁,这将使设备在屏幕关闭时保持唤醒状态。如果另一个应用程序有一个不使用不精确重复的计时器,那么设备也会为此唤醒。
根据您设备上的 Android 版本,您可以在“设置”->“唤醒时间”中找到您的清醒时间图表。关于电话->电池使用 ->然后单击顶部的图表。
有关唤醒锁的更多信息,请访问 http://developer.android.com/reference/android/os /PowerManager.html
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
当没有正在运行的应用程序阻止设备休眠时,设备就处于休眠状态。所以:
1. 屏幕关闭(当屏幕打开时,总是有一些正在运行的应用程序,例如Launcher)
2. 没有正在运行的服务(例如音乐、下载)- 没有CPU 锁定。
AlarmManager 将设备从睡眠状态唤醒,运行要运行的内容。例如,结束广播接收器中创建的服务以使其再次进入睡眠状态是至关重要的。如果您在后台执行较长且重要的操作,您应该为您的应用程序获取 CPU 锁定,以防止它被 Android 操作系统杀死。
“我如何验证从哪个时间不再执行该命令”到底是什么意思?什么命令?
来自 JavaDoc:
因此操作系统保留所有警报,设备可以被睡眠时发出的警报唤醒。警报在重新启动后被删除(您应该将它们保留在数据库中并在重新启动后恢复它们)。
“有没有办法从 logcat 输出中查看设备何时开始休眠?”
据我所知,没有,我只能在屏幕关闭时看到这样的东西:
来自“电源”标签的
*** set_screen_state 0
恕我直言,您不应该担心睡眠模式,只需让您的应用程序“好Android公民”通过处理屏幕关闭和打开意图(Intent.ACTION_SCREEN_OFF)(如果需要),尽快完成服务,尽快释放资源(网络,GPS,CPU),使用不精确的警报,下载管理器以及操作系统带来的所有好员工。我相信 Android 会透明地处理 CPU 睡眠。
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 0
from "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.