0

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

setReferenceCounted

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

cody
  • 6,389
  • 15
  • 52
  • 77
  • There has been a bug that has been filed in this regard... Hope u r not the only one who is raising this question http://code.google.com/p/android/issues/detail?id=5958 – DeRagan Dec 13 '10 at 11:16
  • Ok. So for every release you do on the lock you must have aquired it before to get no exception. I can live with that :) thank you for that advice. – cody Dec 13 '10 at 13:08

1 Answers1

8

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491