Question

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

setReferenceCounted

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top