11

Trying to check Internet connection in my app. There is a code of my manifest:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <receiver android:name=".MyReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </intent-filter>
    </receiver>

And there is a handle class:

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction() != null && intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE"));
    {
        Log.d("myLogs", "Network connectivity change");
        if (intent.getExtras() != null) {
            NetworkInfo ni = (NetworkInfo) intent.getExtras().get(
                    ConnectivityManager.EXTRA_NETWORK_INFO);
            if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
                Log.i("myLogs", "Network " + ni.getTypeName() + " connected");
            }
        }
        if (intent.getExtras().getBoolean(
                ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
            Log.d("myLogs", "There's no network connectivity");
        }
    }
}

}

In my Logcat i get a picture like a:

04-11 23:24:48.021: D/myLogs(10261): Network connectivity change
04-11 23:24:48.021: I/myLogs(10261): Network WIFI connected
04-11 23:24:48.202: D/myLogs(10261): Network connectivity change
04-11 23:24:48.202: I/myLogs(10261): Network WIFI connected

So, receiver called twice. Why? There is some problem with all types of connections.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Sunstrike
  • 456
  • 1
  • 6
  • 22
  • Maybe there are varying levels of being connected, and your 2 logs are 2 different sub-states. I'd log out more info to see how/if they differ. – Steven Byle Apr 11 '13 at 20:53
  • Try to log `if (netInfo != null && netInfo.isConnected()) { Log.i("myLogs", "Network " + netInfo.getTypeName() + " connected"); }` Have idea that there are two sub-state - isConnecting and isConnected, but the same problem/ – Sunstrike Apr 11 '13 at 21:43

2 Answers2

1

You may also need to check to see if the intent being received is one of the Sticky events by checking isStickyBroadcast in your reciever. If that is true, then you may ignore it, and continue on. You get sticky broadcasts as soon as the receiver is registered.

http://developer.android.com/reference/android/content/BroadcastReceiver.html#isInitialStickyBroadcast%28%29

kingargyle
  • 1,239
  • 10
  • 15
0

Explained in the docs:

"Changes to a device's connectivity can be very frequent—this broadcast is triggered every time you move between mobile data and Wi-Fi. As a result, it's good practice to monitor this broadcast only when you've previously suspended updates or downloads in order to resume them. It's generally sufficient to simply check for Internet connectivity before beginning an update and, should there be none, suspend further updates until connectivity is restored."

Edit: Forgot to add...a broadcast receiver only to know if the device is connected is a bit of an hoverhead. From the same docs:

ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
motoDrizzt
  • 1,082
  • 11
  • 23