title: Monitoring internet connectivity everytime app opens in android
body: <p>I am checking the connectivity of the device as suggested in <a href="https://stackoverflow.com/questions/2326767/how-do-you-check-the-internet-connection-in-android">this question</a> with the following code:</p>
  
  <pre><code>final ConnectivityManager conMgr =  (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
  if (activeNetwork != null &amp;&amp; activeNetwork.getState() == NetworkInfo.State.CONNECTED) {
      //notify user you are online
  } else {
      //notify user you are not online
  } 
  </code></pre>
  
  <p>I have this in my main Activity and it works fine, but what I would like to do now is to check the internet connection everytime the application fires up, sometimes the app is left in some state and then the internet is disconnected, so when it opens up again it does not go through the main activity so nothing is checked.</p>
  
  <p>As suggested in <a href="https://stackoverflow.com/questions/7140061/broadcast-receiver-and-internet-connection">this question</a> I have followed <a href="http://thiranjith.com/2011/03/31/how-to-monitor-network-connectivity-in-android/" rel="nofollow noreferrer">this tutorial</a> on monitoring internet activity with BroadcastReceiver and I am trying to display and AlertDialog when noConnectivity is true, but nothing happens.</p>
  
  <p>This is my code, using the tutorial mentioned above:</p>
  
  <pre><code>public class MyActivity extends Activity {
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.welcome);
  
      registerReceivers() ;
           (..my activity code goes here...)
  
  }   
  
      private void noInternet()
      {
          Intent myIntent = new Intent(this, NoInternetActivity.class);
          startActivity(myIntent);
      }
  
      /*
       * method to be invoked to register the receiver
       */
      private void registerReceivers() {    
          registerReceiver(mConnReceiver, 
              new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
      }
  
      private void alert()
      {
          AlertDialog alertDialog = new AlertDialog.Builder(this).create();
          alertDialog.setTitle("internet connection");
          alertDialog.setMessage("To use this app you need intenet connection");
          alertDialog.setButton("ok", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
                // here you can add functions
             }
          });
          alertDialog.show();
      }
  
      private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
              String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
                          boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
  
              NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
              NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
  
              // do application-specific task(s) based on the current network state, such 
              // as enabling queuing of HTTP requests when currentNetworkInfo is connected etc.
  
              if(noConnectivity)
              {
                  alert();
                  noInternet();
              }
  
          }
      };
  }
  </code></pre>
  
  <p>but neither <code>alert()</code> or <code>noInternet()</code> gets fired up.</p>
  
  <p>Hope you can help me out.
  Thanks</p>