gpt4 book ai didi

android - 初学者问题: Wakelocks

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:55:57 27 4
gpt4 key购买 nike

我是 WakeLock 概念的新手,需要您的帮助。

问题:

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

  2. 当一个长时间运行的任务(大约 7-8 秒)在后台线程 (AsyncTask) 中完成时,我是否应该为保持唤醒锁而烦恼? AsyncTask 是否已经为我获取了它?

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

谢谢。

最佳答案

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

来自AlarmManager类文档:

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.如果您使用的是 AsyncTask ,您将希望将结果发布到 onPostExecute() 上的 UI 线程上

来自AsyncTask文档:

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.建议你看一下Power Manager的官方文档这很好地介绍了 WakeLock 概念。

关于android - 初学者问题: Wakelocks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4252434/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com