registerReceiver when there is no connection and unregisterReceiver when connection is available
<p>I am writing an application that updates its data periodically, such as every hour.
If there is no internet connection available, I want to subscribe to the notification that the connection is available and then update. Then if update is successfull i want to unregister from this notifications.
How can i do this?</p>

<p>To update data I'm calling service:</p>

<pre><code>Intent updateIntent = new Intent(activity, UpdateService.class);
activity.startService(updateIntent);
</code></pre>

<p>I'm doing update in my UpdateService class:</p>

<pre><code>public class UpdateService extends IntentService
{
    protected void onHandleIntent(Intent intent)
    {
         // Here I'm checking if connection is available, if not
         // I want to register for notifications
    }
}
</code></pre>

<p>Here is my NetworkStateChangeReceiver class:</p>

<pre><code>public class NetworkStateChangeReceiver extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        boolean isNetworkConnected = activeNetworkInfo != null &amp;&amp; activeNetworkInfo.isConnected();

        if(isNetworkConnected) 
        {
            Intent updateIntent = new Intent(context, UpdateService.class);
            context.startService(updateIntent);
        }
    }
}
</code></pre>

<p>I don't want to register my receiver in the manifest file, because then I'll receive notifications even if I don't need them.</p>

<p>Thanks for any advice</p>
