How to monitor connectivity changes when nothing else is running
<p>I have several timers that poll information off the internet. When I get an error trying to do so, I check the network connectivity, and if it is not available, then I stop the timers.  My issue is that I want to restart the timers when the network becomes available again.</p>

<p>The issue is that if the network goes away and doesn't come back for a long time, then probably every part of my app that was running has now been killed or so.  My question is...where do I create my network connectivity changes receiver so that it will always be able to respond to those events?  Obviously not in an activity.</p>

<p>I had originally put it in an extended Application class.  I'm not sure if that was correct, and I'm finding now that even that goes away, but gets created again any time some part of your app gets utilized. So is that still the correct place?</p>

<p>I'm also a bit confused...I had thought to create a standalone receiver class to monitor the network changes and then restart the timers if it got a connection message.  But then I thought....what would instantiate this?  If nothing did, then how would it work?  But the same question then goes to my OnBootReceived class....I do nothing to instantiate that class anywhere and register it, yet it still works!  (Other than putting it in the  AndroidManifest and the BOOT_COMPLETED intent filter).  Is that kind of receiver special?  I tried doing the same thing with my network change receiver, but it never responded to any network changes.  When I finally put it in my application class, instantiated it, and registered it, THEN it received the broadcasts.</p>

<p>I have code, but this is kind of a code-independent question.  I'm trying to clear up WHERE my code goes.  IF I get past that and it still doesn't work, I can post it.</p>

<p>So, I'm <em>almost</em> understanding.  If I do nothing more than have this in my code, will I receive messages when the connectivity state changes? Because if so, I'm <em>not</em> receiving any and this is what I have.</p>

<p>Or do I need to register this on boot somehow?  The video mentions using the manifest intent filter to get updates when your app isn't running, but doesn't show if it still needs to be registered somehow somewhere (and if you DO register, aren't you registering an instance, and if so, won't that instance disappear when whatever object you were registering it in goes away?)</p>

<p>Code:</p>

<pre><code>public class NetworkConnectivityReceiver extends BroadcastReceiver {

    Context context;

    @Override
    public void onReceive(Context context, Intent intent) {



        Log.d(TAG, "Connection message received.");

    }

}


    &lt;receiver android:name=".NetworkConnectivityReceiver" &gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION"&gt;&lt;/action&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;
</code></pre>
