title: Checking Online Connectivity Always Returns True
body: <p>I have a dynamic Broadcast Receiver set up in my Main Activity that should display a Toast message whenever my Network State Changes. I connect online, run the application, the "You are online!" Toast message displays, I then turn the internet off on my laptop, navigate back to my Main Activity and I keep getting the same "You are online!" Toast. I'm checking my log statements and my NetworkInfo Object Keeps returning that I am indeed online when I'm clearly not. I tried this on my emulator and my phone. Does anyone know why?</p>
  
  <p>Here is my Main Activity which contains my nested Broadcast Receiver Class, Intent Filters and Method to execute the Toast:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  private IntentFilter onlineIntentFilter;
  private CheckOnlineReceiver checkOnlineReceiver;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      onlineIntentFilter = new IntentFilter();
      checkOnlineReceiver = new CheckOnlineReceiver();
      onlineIntentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
  
      }
  
  
  @Override
  protected void onResume() {
      super.onResume();
      registerReceiver(checkOnlineReceiver, onlineIntentFilter);
  }
  
  @Override
  protected void onPause() {
      super.onPause();
      unregisterReceiver(checkOnlineReceiver);
  }
  
  
  public void isNetworkAvailable() {
      ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo networkInfo = cm.getActiveNetworkInfo();
      Log.v("TAG", "NETWORK INFO =========== " + networkInfo);
      if (networkInfo != null &amp;&amp; networkInfo.isConnected()) {
          Toast.makeText(this,"You Are Online!",Toast.LENGTH_LONG).show();
      }else{
          Toast.makeText(this,"You Are Not Online!",Toast.LENGTH_LONG).show();
      }
  }
  private class CheckOnlineReceiver extends BroadcastReceiver{
  
      @Override
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          boolean stateChanged = (action.equals(ConnectivityManager.CONNECTIVITY_ACTION));
          if (stateChanged){
              isNetworkAvailable();
          }
      }
  }}
  </code></pre>