唤醒锁参考计数
Can anyone explain what this method of PowerManager.Wakelock is for?
"Wake locks are reference counted by default." - why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Can anyone explain what this method of PowerManager.Wakelock is for?
"Wake locks are reference counted by default." - why?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
因为在很多情况下对它们进行引用计数确实很方便。
以
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-countedWakeLock
, 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 theWakeLock
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.