初学者问题:唤醒锁

发布于 2024-10-04 09:47:47 字数 306 浏览 3 评论 0 原文

我对 WakeLock 的概念很陌生,需要您的帮助。

问题:

  1. 我假设 WakeLock 是某种类型的锁,当执行代码持有该锁时,会阻止设备休眠。如果设备已经处于睡眠/待机模式,代码会执行吗?假设它永远不会获取 WakeLock?

  2. 当一个长时间运行的任务(大约 7-8 秒)在后台线程(AsyncTask)中完成时,我应该担心持有 WakeLock 吗? AsyncTask 是否已为我获取它?

  3. 欢迎链接到官方文档和有关唤醒锁的文章。

谢谢。

I am new to the notion of WakeLock and need your help.

Questions:

  1. 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?

  2. 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?

  3. links to official documentations and writeup on wakelocks are appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戴着白色围巾的女孩 2024-10-11 09:47:47

1.如果手机处于完全睡眠模式,除了来电之外,您可以使用 AlarmManager Intent 来唤醒手机。

来自 AlarmManager 类文档:

警报管理器保持CPU唤醒
只要报警接收器的锁定
onReceive() 方法正在执行。这
保证手机不会
睡觉直到处理完毕
广播。一旦 onReceive()
返回,警报管理器释放
这个唤醒锁。这意味着
在某些情况下,手机会尽快进入睡眠状态
当你的 onReceive() 方法完成时。
如果您的报警接收器打来电话
Context.startService(),是可以的
手机会在
请求的服务已启动。到
防止这种情况发生,你的广播接收器
和服务将需要实施
单独的唤醒锁策略以确保
电话继续运行直到
该服务可用。


2.如果您正在使用 AsyncTask,您将需要将结果发布到 onPostExecute( )

来自 AsyncTask 文档:

AsyncTask 可以正确且轻松地使用 UI 线程。此类允许在 UI 线程上执行后台操作并发布结果,而无需操作线程和/或处理程序。

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:

The Alarm Manager holds a CPU wake
lock
as long as the alarm receiver's
onReceive() method is executing. This
guarantees that the phone will not
sleep until you have finished handling
the broadcast. Once onReceive()
returns, the Alarm Manager releases
this wake lock. This means that the
phone will in some cases sleep as soon
as your onReceive() method completes.
If your alarm receiver called
Context.startService(), it is possible
that the phone will sleep before the
requested service is launched. To
prevent this, your BroadcastReceiver
and Service will need to implement a
separate wake lock policy to ensure
that the phone continues running until
the service becomes available.

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:

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

3.I suggest you have a read at the official documentation of Power Manager which gives a good introduction to the WakeLock concept.

皓月长歌 2024-10-11 09:47:47
  1. 通常,在手机睡眠时运行的唯一代码是 BroadcastReceiver。实际上,手机会唤醒一秒钟,运行 BroadcastReceiver 的代码,然后再次休眠。由于您永远不应该在 BroadcastReceiver 中运行长代码(而是使用它来启动 Service),因此您基本上可以假设您的代码在手机睡眠时永远不会运行。当然,如果您使用 BroadcastReceiver 启动 Service,您通常应该获取 WakeLock。

  2. 通过用户启动的AsyncTask,您实际上不需要担心WakeLocks。手机在运行时不太可能进入睡眠状态。我不确定他们是否获得了 WakeLock,但是在运行标准 AsyncTask 时让我的手机进入睡眠状态似乎不会中断它。

  3. 正如 SteD 所说,请查看:http://developer.android。 com/reference/android/os/PowerManager.html

基本上,您需要担心 WakeLocks 的唯一时间是当您希望您的任务因睡眠而中断(如果您设置了短暂唤醒手机的闹钟,就会出现这种情况)或者您绝对不能让任务中断。否则,只需确保妥善处理任何干扰即可。

  1. Typically the only code that would run while the phone is sleeping is a BroadcastReceiver. Actually, the phone wakes up for a second, runs the BroadcastReceiver's code and sleeps again. Since you should never run long code in a BroadcastReceiver (use it to launch a Service instead), you can basically assume that your code is never run while the phone is sleeping. Of course, if you are using a BroadcastReceiver to start a Service, you should usually obtain a WakeLock.

  2. 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 standard AsyncTask doesn't seem to interrupt the it.

  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文