title: Broadcast receiver for checking internet connection in android app
body: <p>I am developing an android broadcast receiver for checking internet connection.</p>
  
  <p>The problem is that my broadcast receiver is being called two times. I want it to get called only when the network is available. If it is unavailable, I don't want notified.</p>
  
  <p>This is the broadcast receiver</p>
  
  <pre><code>public class NetworkChangeReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(final Context context, final Intent intent) {
          final ConnectivityManager connMgr = (ConnectivityManager) context
                  .getSystemService(Context.CONNECTIVITY_SERVICE);
  
          final android.net.NetworkInfo wifi = connMgr
                  .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  
          final android.net.NetworkInfo mobile = connMgr
                  .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
  
          if (wifi.isAvailable() || mobile.isAvailable()) {
              // Do something
  
              Log.d("Network Available ", "Flag No 1");
          }
      }
  }
  </code></pre>
  
  <p>This is the manifest.xml</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.broadcastreceiverforinternetconnection"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;receiver android:name=".NetworkChangeReceiver" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt;
                  &lt;action android:name="android.net.wifi.WIFI_STATE_CHANGED" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>