开发者

Wakelock Reference Count

开发者 https://www.devze.com 2023-01-30 01:53 出处:网络
Can anyone explain what this method of开发者_开发问答 PowerManager.Wakelock is for? setReferenceCounted

Can anyone explain what this method of开发者_开发问答 PowerManager.Wakelock is for?

setReferenceCounted

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消