Android BroadcastReceiver wait for Service to start or transmit WakeLock to Service?
<ol>
<li>BroadcastReceiver receives a broadcast, opens a wakelock, starts a Service</li>
<li>Service opens another wakelock, then sends a "release wakelock" broadcast</li>
<li>BroadcastReceiver receives the release wakelock broadcast and releases its wakelock</li>
<li>Service does its thing and releases its wakelock</li>
</ol>

<p>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).</p>

<p>So I need to, in the onReceive method, do one of the following:</p>

<ul>
<li>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</li>
<li>or, somehow transmit the wakelock to the service, and then the service will be responsible for releasing the single wakelock</li>
</ul>

<p><strong>Which one is the better option and how would I accomplish it?</strong></p>
