Search code examples
androidwifibroadcastconnectivityreceiver

BroadCast CONNECTIVITY_CHANGE Triggered Many Times


In a BroadcastReceiever I am using this code to detect when the device is connected to the internet (when a connection is available - WiFi/data):

if(arg1.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {

        ConnectivityManager connMgr = (ConnectivityManager) arg0.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        if(networkInfo != null && networkInfo.isConnected() && networkInfo.isAvailable()) {

 Toast.makeText(Context, "Connected to internet", Toast.LENGTH_LONG).show();

 }

The problem:

  1. Phone is disconnected

  2. I enable WiFi or data to connect

  3. I receive the notification but too many times. The phone is still connected but I keep receiving notifications.

Why?

Thanks


Solution

  • You can try this:

    public class MainActivity extends Activity {
    
        private class ConnectivityBroadcastReceiver extends BroadcastReceiver {
    
            @Override
            public void onReceive( final Context context, final Intent intent ) {
    
                MainActivity.this.oldWifiConnectionState = MainActivity.this.currentWifiConnectionState;
    
                boolean wifiConnected = false;
    
                final ConnectivityManager connMgr = ( ConnectivityManager ) context.getSystemService( Context.CONNECTIVITY_SERVICE );
    
                final NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    
                if ( ( networkInfo != null ) && networkInfo.isConnected() && networkInfo.isAvailable() ) {
                    wifiConnected = true;
                }
    
               // or you can use 
               // wifiConnected  = ( activeNetwork != null ) && activeNetwork.isConnectedOrConnecting();
    
    
                boolean mobileDataEnabled = false; // Assume disabled
    
                try {
                    final Class cmClass = Class.forName( connMgr.getClass().getName() );
                    final Method method = cmClass.getDeclaredMethod( "getMobileDataEnabled" );
                    method.setAccessible( true ); // Make the method callable
    
                    // get the setting for "mobile data"
                    mobileDataEnabled = ( Boolean ) method.invoke( connMgr );
    
                } catch ( final Exception e ) {
                    mobileDataEnabled = false;
                }
    
                MainActivity.this.currentWifiConnectionState = ( wifiConnected || mobileDataEnabled ) ? WifiConnectionState.CONNECTED : WifiConnectionState.NOT_CONNECTED;
    
                if ( !MainActivity.this.currentWifiConnectionState.equals( MainActivity.this.oldWifiConnectionState ) ) {
                    // Notifiy any handlers.
    
                }
    
            }
    
        }
    
        private enum WifiConnectionState {
            CONNECTED, NOT_CONNECTED, UNKNOWN
        }
    
        private WifiConnectionState           currentWifiConnectionState;
    
        private ConnectivityBroadcastReceiver mReceiver = null;
    
        private WifiConnectionState           oldWifiConnectionState;
    
        @Override
        protected void onCreate( final Bundle savedInstanceState ) {
    
            this.currentWifiConnectionState = WifiConnectionState.UNKNOWN;
    
            this.mReceiver = new ConnectivityBroadcastReceiver();
    
            super.onCreate( savedInstanceState );
    
        }
    
        @Override
        protected void onPause() {
    
            this.unregisterReceiver( this.mReceiver );
            super.onPause();
        }
    
        @Override
        protected void onResume() {
            this.registerReceiver( this.mReceiver, new IntentFilter( "android.net.conn.CONNECTIVITY_CHANGE" ) );
            super.onResume();
        }
    }