When the connection to the Internet is changed, the onReceive method works 2 times (One response is required).

public class NetworkChangeReceiver extends BroadcastReceiver { boolean connection; @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if ((wifi != null && wifi.isConnectedOrConnecting()) || (mobile != null && mobile.isConnectedOrConnecting())){ connection = true; } if (connection) { Log.d("Network Available ", "YES"); } else { Log.d("Network Available ", "NO"); } } 

}

The result in the log when you turn off:

  D/Network Available: NO D/Network Available: NO 

Accordingly, when you turn on:

  D/Network Available: YES `D/Network Available: YES 
  • one
    How did you declare it in the manifesto? - Yury Pashkov

1 answer 1

Your broadcastReciever works twice, because you most likely have two <intent-filter> to change the Internet connection

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

and to change the state of wi-fi

 "<action android:name="android.net.wifi.WIFI_STATE_CHANGED" 

use only one

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

This should solve your problem, the receiver will only work when the state of the Internet connection changes

  • one
    I have one intent-filter in the manifesto - GitHors