2

I'm developing an android application and I have the next problem: I implemented Broadcast receiver for connectivity change and the method onReceive seems to be called 4 times in a row when 3G and Wifi are enabled at the same time.

So my question is:
Is there a way listen only for internet connection, not for network change?
Or is there any way for the method onReceive to be called only once when 3G and Wifi are enable at the same time?

Here is my code:

public class NetworkChangeReceiver extends BroadcastReceiver {

    public static final String TAG = "NetworkMonitoring";

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

        if (isOnline(context)) {

            Log.v(TAG, "Connected!");
            // update(context);
        } else {

            Log.v(TAG, "Not connected!");
            // stopUpdate(context);

        }
    }

    public boolean isOnline(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected())
            return true;
        return false;

    }

}

In the Android Manifest:

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

Here is the log:

05-06 16:24:05.985: V/NetworkMonitoring(569): Connected!
05-06 16:24:10.250: V/NetworkMonitoring(569): Connected!
05-06 16:24:10.720: V/NetworkMonitoring(569): Connected!
05-06 16:24:11.031: V/NetworkMonitoring(569): Connected!

(Notice the time!)

nikki
  • 43
  • 7

1 Answers1

6

I had the same problem with the same type of broadcast receiver. Searched a little and found a workaround.

BroadcastReceiver receives multiple identical messages for one event

Edit: The workaround is to use a flag that tells you when is the first time onReceive is being invoked.

public class ConnectionChangeReceiver extends BroadcastReceiver {
    private static boolean firstConnect = true;

    @Override
    public void onReceive(Context context, Intent intent) {
        final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null) {
            if(firstConnect) { 
                // do subroutines here
                firstConnect = false;
            }
        }
        else {
            firstConnect= true;
        }
    }
}

Hope it helps.

Community
  • 1
  • 1
Hristova
  • 156
  • 1
  • 6
  • 1
    Really appreciate your effort of posting and taking time to share a solution you have found. But unfortunately this can't be answer as it is just a link to an existing SO thread. It should be comment! – Paresh Mayani Jul 22 '14 at 12:35
  • Well, this question was added 2 months ago, while there is an accepted answer to the same question (from 2 years ago) in the link I posted. @PareshMayani I don't really understand what you want me to do. Please, explain as I'm new to participating actively in this site. – Hristova Jul 22 '14 at 12:40
  • Answering in links is discouraged in Stack overflow. Provide the portion of that page that answers the question of OP in your answer so that future readers may be able to read it even if the link is broken. And in this case, it's just a link to an existing Stackoverflow thread, so you should leave a comment (but again for unlocking commenting privilege, you would require minimum 50 reputations). – Paresh Mayani Jul 22 '14 at 12:44