- BroadcastReceiver receives a broadcast, opens a wakelock, starts a Service
- Service opens another wakelock, then sends a "release wakelock" broadcast
- BroadcastReceiver receives the release wakelock broadcast and releases its wakelock
- Service does its thing and releases its wakelock
There's a problem though. Currently the BroadcastReceiver stores its WakeLock as a member variable. Sometimes the garbage collector will run after the BroadcastReceiver starts the service but before it receives the release wakelock broadcast, and then I get a force close because the wakelock is finalized before it is released (since it gets garbage collected).
So I need to, in the onReceive method, do one of the following:
- wait for the service to start, wait for it to establish its wakelock, and then release the BroadcastReceiver wakelock; the 'release wakelock' broadcast will no longer be necessary
- or, somehow transmit the wakelock to the service, and then the service will be responsible for releasing the single wakelock
Which one is the better option and how would I accomplish it?