Proper Way to Retry Network Communication
<p>I'm building an app that periodically retrieves data from a website using a recurring alarm and an IntentService.  Before running the update, I check for network connectivity with a ConnectivityManager.</p>

<p>If the ConnectivityManager reports that there is no active network, I wish to try the update again when a network becomes active.  Using a BroadcastReceiver listening for ConnectivityManager.CONNECTIVITY_ACTION seems like the best way to do this, but what would be the proper way to register/unregister this receiver?</p>

<p>I'm thinking that when the network check fails in the IntentService, that I could register the BroadcastReceiver with the ApplicationContext so it doesn't get unregistered when the service stops.  Could I then unregister the receiver in its own onReceive method when it fires?  Am I way off base?</p>

<p>Some psuedo code examples:</p>

<p>MyIntentService:</p>

<pre><code>if ( !isNetworkConnected() )
    getApplicationContext().registerReceiver( new MyBroadcastReceiver(), new IntentFilter( ConnectivityManager.CONNECTIVITY_ACTION );
</code></pre>

<p>MyBroadcastReceiver:</p>

<pre><code>public void onReceive( Context context, Intent intent ) {
    context.getApplicationContext().unregisterReceiver( this );
</code></pre>
