Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver on android.net.conn.CONNECTIVITY_CHANGE called multiple times

I have the following in my manifest

 <receiver android:name=".receiver.WifiReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
 </receiver>

and the following BroadcastReceiver:

public class WifiReceiver extends BroadcastReceiver { 
private static String TAG = makeLogTag(WifiReceiver.class);

@Override
public void onReceive(Context context, Intent intent) {

    ConnectivityManager connectivityManager = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (connectivityManager != null) {
        networkInfo =
                connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        LOGD(TAG, "connectivity info:" + networkInfo);
    }

   if(networkInfo != null && networkInfo.isConnected()) {
       //TODO: see why this is called multiple times and handle schedule reloading
       LOGD(TAG, "have Wifi connection and is connected");
   }else
       LOGD(TAG, "don't have Wifi connect or it isn't connected");
}

When i switch from mobile to wifi the receiver get called multiple times (no problem there) but the if(networkInfo != null && networkInfo.isConnected()) branch evaluates to true all 4 times

like image 573
forcewill Avatar asked Oct 06 '13 09:10

forcewill


People also ask

How do I stop BroadcastReceiver?

To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.

What is BroadcastReceiver Android?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.

What is BroadcastReceiver?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

How do I register for BroadcastReceiver?

This example demonstrates how do I register a BroadcastReceiver programtically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

public class NetworkChangeReceiver extends BroadcastReceiver {
Context mContext;

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;
    String status = NetworkUtil.getConnectivityStatusString(context);

    Log.e("Receiver ", "" + status);

    if (status.equals("Not connected to Internet")) {
        Log.e("Receiver ", "not connction");// your code when internet lost


    } else {
        Log.e("Receiver ", "connected to internet");//your code when internet connection come back
    }

}

}

and use this call for check wifi or internet conenctivity lost

public class NetworkUtil {

public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;

public static int getConnectivityStatus(Context context) {

    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

    if (null != activeNetwork) {

        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return TYPE_WIFI;

        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return TYPE_MOBILE;
    }
    return TYPE_NOT_CONNECTED;
}

public static String getConnectivityStatusString(Context context) {

    int conn = NetworkUtil.getConnectivityStatus(context);

    String status = null;
    if (conn == NetworkUtil.TYPE_WIFI) {
        //status = "Wifi enabled";
        status="Internet connection available";
    } else if (conn == NetworkUtil.TYPE_MOBILE) {
        //status = "Mobile data enabled";
        status="Internet connection available";
    } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
        status = "Not connected to Internet";
    }
    return status;
}

}

and add this in your androidmanifest file

<receiver
        android:name=".NetworkChangeReceiver"
        android:label="NetworkChangeReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>

Add internet permission in manifest-file:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

i hope it's work for you and

like image 124
Vijay Rajput Avatar answered Sep 24 '22 00:09

Vijay Rajput


It's strange. Anyway try this little bit different code:

ConnectivityManager cm =
    (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if(activeNetwork != null){
    boolean isConnected = activeNetwork.isConnected();
    boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;

    if(isConnected && isWiFi){

    }
}
like image 28
M-WaJeEh Avatar answered Sep 23 '22 00:09

M-WaJeEh