Search code examples
androidandroid-activitybroadcastreceiverandroid-broadcastandroid-networking

Android BroadcastReceiver for internet connection called twice


I am facing this issue for a while and noticed that a few other people also faced it, but I am not able to resolve it.

The Problem

I have a BroadcastReceiver, which keeps listening for the internet changes. So whenever the internet is connected or disconnected, I get the status in onReceive of the receiver.

The problem is it is always getting called twice. When I connect the debugger and have the breakpoint in the code, I can see it getting called twice but my code on status change executes. When the debugger is not connected, in that case my code on status change doesn't execute at all.

My Code

BroadcastReceiver file

public class ConnectivityReceiver
    extends BroadcastReceiver {

public static ConnectivityReceiverListener connectivityReceiverListener;

public ConnectivityReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent arg1) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();

    if (connectivityReceiverListener != null) {
        connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
    }
}

public static boolean isConnected() {
    ConnectivityManager
            cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();
}


public interface ConnectivityReceiverListener {
    void onNetworkConnectionChanged(boolean isConnected);
}

}

Manifest intent action

<receiver
        android:name=".utils.ConnectivityReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>

here when I tried with only <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> in filter, then also it came to onRecieve twice. And when I tried with <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />, then I didn't come to onReceive at all.

BaseActivity

 @Override
public void onNetworkConnectionChanged(boolean isConnected) {
    setNetworkIndicator(isConnected); //THE CODE TO EXECUTE ON STATUS CHANGE
}

I tried the existing answers, and it didn't work for me.


Solution

  • Check this link

    For Android 7 and above you need to register your receiver in your activity file not in the manifest file.

    So in your activity's onCreate() add the following lines:

        myConnectivityReceiver = new ConnectivityReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(getResources().getString(R.string.action_connectivity_change));
        registerReceiver(myConnectivityReceiver,filter);
    

    and in onDestroy()

      @Override
      protected void onDestroy() {
          unregisterReceiver(myConnectivityReceiver);
          super.onDestroy();
      }
    

    define the intent action in strings.xml

    <string name="action_connectivity_change">android.net.conn.CONNECTIVITY_CHANGE</string>