唤醒锁参考计数

发布于 2024-10-08 00:01:21 字数 275 浏览 0 评论 0原文

谁能解释一下 PowerManager.Wakelock 的这个方法的用途是什么?

setReferenceCounted

"唤醒锁默认采用引用计数。” - 为什么?

Can anyone explain what this method of PowerManager.Wakelock is for?

setReferenceCounted

"Wake locks are reference counted by default." - why?

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

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

发布评论

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

评论(1

千寻… 2024-10-15 00:01:21

因为在很多情况下对它们进行引用计数确实很方便。

WakefulIntentService 为例。我需要安排服务让设备保持足够长的唤醒时间,以完成发送给它的任何工作,但是一旦工作完成,设备就可以重新进入睡眠状态。最简单的方法是使用引用计数的 WakeLock,因此我们会增加每项工作的引用计数,并在工作完成时减少引用计数。当引用计数达到零时,Android 会释放 WakeLock 并且设备可以重新进入睡眠状态。

在某些情况下,非引用计数的 WakeLock 可能会很有用,尽管我还没有准备好这样的示例。

Because it is really handy in many cases to have them be reference counted.

Take WakefulIntentService as an example. I need to arrange for the service to keep the device awake long enough to do whatever work is sent its way, but once the work is done, the device can fall back asleep. The easiest way to do that is to use a reference-counted WakeLock, so we bump the reference count for each piece of work and decrement the reference count when the work is done. When the reference count reaches zero, Android releases the WakeLock and the device can fall back asleep.

There may well be scenarios where a non-reference-counted WakeLock would be useful, though I do not have an example of that at the ready.

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