初学者问题:唤醒锁
我对 WakeLock 的概念很陌生,需要您的帮助。
问题:
我假设 WakeLock 是某种类型的锁,当执行代码持有该锁时,会阻止设备休眠。如果设备已经处于睡眠/待机模式,代码会执行吗?假设它永远不会获取 WakeLock?
当一个长时间运行的任务(大约 7-8 秒)在后台线程(AsyncTask)中完成时,我应该担心持有 WakeLock 吗? AsyncTask 是否已为我获取它?
欢迎链接到官方文档和有关唤醒锁的文章。
谢谢。
I am new to the notion of WakeLock and need your help.
Questions:
I assume WakeLock to be some type of lock which when held by the executing code prevents the device from sleeping. What if the device is already in sleep/standby mode, will the code execute then? Assuming that it would never acquire a WakeLock?
When a long running task(abt 7-8 sec) is done in a background thread(AsyncTask) should I be bothered about holding a WakeLock? Does AsyncTask already acquire it for me?
links to official documentations and writeup on wakelocks are appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1.如果手机处于完全睡眠模式,除了来电之外,您可以使用 AlarmManager Intent 来唤醒手机。
来自 AlarmManager 类文档:
2.如果您正在使用 AsyncTask,您将需要将结果发布到
onPostExecute( )
来自 AsyncTask 文档:
3.建议您阅读Power Manager的官方文档它很好地介绍了 WakeLock 概念。
1.If the phone is in full sleep mode, aside from an incoming phone call, you could use an AlarmManager intent to wake the phone up.
From the AlarmManager class documentation:
2.If you're working with an AsyncTask, you will want to publish results on to the UI thread on
onPostExecute()
From the AsyncTask documentation:
3.I suggest you have a read at the official documentation of Power Manager which gives a good introduction to the WakeLock concept.
通常,在手机睡眠时运行的唯一代码是
BroadcastReceiver
。实际上,手机会唤醒一秒钟,运行BroadcastReceiver
的代码,然后再次休眠。由于您永远不应该在BroadcastReceiver
中运行长代码(而是使用它来启动Service
),因此您基本上可以假设您的代码在手机睡眠时永远不会运行。当然,如果您使用BroadcastReceiver
启动Service
,您通常应该获取 WakeLock。通过用户启动的
AsyncTask
,您实际上不需要担心WakeLocks。手机在运行时不太可能进入睡眠状态。我不确定他们是否获得了 WakeLock,但是在运行标准AsyncTask
时让我的手机进入睡眠状态似乎不会中断它。正如 SteD 所说,请查看:http://developer.android。 com/reference/android/os/PowerManager.html
基本上,您需要担心 WakeLocks 的唯一时间是当您希望您的任务因睡眠而中断(如果您设置了短暂唤醒手机的闹钟,就会出现这种情况)或者您绝对不能让任务中断。否则,只需确保妥善处理任何干扰即可。
Typically the only code that would run while the phone is sleeping is a
BroadcastReceiver
. Actually, the phone wakes up for a second, runs theBroadcastReceiver
's code and sleeps again. Since you should never run long code in aBroadcastReceiver
(use it to launch aService
instead), you can basically assume that your code is never run while the phone is sleeping. Of course, if you are using aBroadcastReceiver
to start aService
, you should usually obtain a WakeLock.With an
AsyncTask
initiated by the user, you don't really need to worry about WakeLocks. It is unlikely the phone will sleep while it is running. I'm not sure if they get a WakeLock, but putting my phone to sleep while running a standardAsyncTask
doesn't seem to interrupt the it.As SteD said, check this out: http://developer.android.com/reference/android/os/PowerManager.html
Basically the only time you need to worry about WakeLocks is when you either expect your task to be interrupted by sleeping (as is the case if you set an alarm that wakes the phone up briefly) or if you absolutley cannot have the task interrupted. Otherwise, just make sure that you gracefully handle any interruptions.