title: Android: BroadcastReceiver for internet connection
body: <p>I am trying to get my <code>BroadcastReceiver</code> to work but I have the problem when I disconnect the internet connection, I am getting the <code>Toast</code> "Please connect the device to the internet.", on the other hand I am getting the same <code>Toast</code> "Please connect the device to the internet." When I establish internet connection again. Is something wrong with the class?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>package com.bustracker;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo;
  import android.util.Log;
  import android.widget.Toast;
  
  public class ConnectionChangeReceiver extends BroadcastReceiver {
  
   @Override
   public void onReceive(Context context, Intent intent) {
  
    ConnectivityManager connectivityManager = (ConnectivityManager) context
      .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager
      .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    boolean isConnected = activeNetInfo != null
      &amp;&amp; activeNetInfo.isConnectedOrConnecting();
    if (isConnected) {
     Toast.makeText(context, "The device is connected to the internet ",
       Toast.LENGTH_SHORT).show();
     Log.i("NET", "connecte" +isConnected);
    } else {
     Toast.makeText(context, "Please connect the device to the internet.",
       Toast.LENGTH_SHORT).show();
    }
  
   }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>        &lt;receiver android:name=".ConnectionChangeReceiver" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;</code></pre>
  </div>
  </div>
  </p>