javaandroidbroadcastreceiverandroid-wifiandroid-networking

Broadcast receiver for checking internet connection in android app


I am developing an Android broadcast receiver for checking the internet connection.

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 to be notified.

This is the broadcast receiver

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");
        }
    }
}

This is the manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcastreceiverforinternetconnection"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".NetworkChangeReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

Solution

  • Answer to your first question: Your broadcast receiver is being called two times because

    You have added two <intent-filter>

    1. Change in network connection :
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

    2. Change in WiFi state:
      <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />

    Just use one:
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />.

    It will respond to only one action instead of two. See here for more information.

    Answer to your second question (you want the receiver to call only one time if an internet connection is available):

    Your code is perfect; you notify only when the internet is available.

    UPDATE

    You can use this method to check your connectivity if you want just to check whether your mobile is connected to the internet or not.

    public boolean isOnline(Context context) {
      
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        //should check null because in airplane mode it will be a null
        return (netInfo != null && netInfo.isConnected());
    }