0

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

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99

1 Answers1

0

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();
    }
}
wbelarmino
  • 509
  • 3
  • 7
  • Thanks for your Answer but this is pretty the Same Code than Mine, Try to Open your Activity, Enable Wifi & you will see its triggered Many Times(by using a Toast for example), this is a Bug from Android itself ? the BroadCast is sent many times after the Device get Connected, & i tried with all Methods available(isAvailable() etc...) –  Jul 24 '14 at 15:41
  • Time to time your broadcast will receive this notification you can use "enum WifiConnectionState" to control this. – wbelarmino Jul 24 '14 at 15:52
  • Sorry, but what is "enum WifiConnectionState" ? –  Jul 24 '14 at 16:01
  • In my code i create an enum called "WifiConnectionState" to control the wifi state(connected, not connected) and notify all handlers if that state has changed. – wbelarmino Jul 24 '14 at 16:04