38

I need to receive broadcasts for network actions like network connected, disconnected etc. I am using a broadcast receiver for this purpose. Can anyone please tell me which intent action I need to capture for network events, right now as per my search on internet I am using android.net.ConnectivityManager.CONNECTIVITY_ACTION.

Here is my broadcast receiver class:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class NetworkStateReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub


    if (intent.getAction().equals(
            android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {

        // do something..
    }
}
}

and I have also added permission for accessing network state:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

here is how I have declared this class in manifest file

<receiver class=".NetworkStateReceiver" android:name=".NetworkStateReceiver">
    <intent-filter>
            <action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
    </intent-filter>
</receiver>

Please suggest me the right intent action if I am wrong OR if there is any other way to catch network events.

Janusz
  • 187,060
  • 113
  • 301
  • 369
mudit
  • 25,306
  • 32
  • 90
  • 132

2 Answers2

51

Here's a working example:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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

.

public class ConnectivityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(ConnectivityReceiver.class.getSimpleName(), "action: "
                + intent.getAction());
    }

}
Informatic0re
  • 6,300
  • 5
  • 41
  • 56
yanchenko
  • 56,576
  • 33
  • 147
  • 165
  • According to my test I don't need any permission to receive broadcasts whether wlan is now up/down or 3g is now up/down. I am a little bit confused, what is android.net.conn.CONNECTIVITY_CHANGE then good for? – stefan.at.kotlin May 11 '11 at 18:08
  • If you look again, you'll see that the CONNECTIVITY_CHANGE isn't a permission, it's registering an intent receiver (which you can alternatively do in code). – Hamid Apr 12 '12 at 12:25
  • The answer looks the same as the the question. So why did it NOT work before the answer? – AlikElzin-kilaka Jul 20 '12 at 21:19
  • 2
    This answer just fix the intent action name in the manifest. the **value** of `android.net.ConnectivityManager.CONNECTIVITY_ACTION` is `android.net.conn.CONNECTIVITY_CHANGE` and it's this one which match the intent action. – FabiF Sep 04 '12 at 11:56
  • 3
    This code works, but it only tells you "connectivity change" is there a way to tell if it's connected or dissconnected? – Evan R. Sep 16 '12 at 21:43
6

yanchenko's answer is very much useful, i am just simplifying a little bit to get connection status, please modify onReceive as below :

public class ConnectivityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(ConnectivityReceiver.class.getSimpleName(), "action: "
                + intent.getAction());
        MyConstants.IS_NETWORK_AVAILABLE = haveNetworkConnection(context);
        //IS_NETWORK_AVAILABLE this variable in your activities to check networkavailability.

    } 


    private boolean haveNetworkConnection(Context context) {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager)   context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;    
    }
}
aweis
  • 5,350
  • 4
  • 30
  • 46
Ravi K. Sharma
  • 545
  • 8
  • 15
  • at Ravi K Sharma, are you aware of android.net.ConnectivityManager.EXTRA_NO_CONNECTIVITY? That's an extra boolean in the Intent extras since API level 1 which tells whether there is connectivity or not. – class stacker Dec 15 '12 at 17:01
  • 1
    Regarding yesterday's comment, I noticed it was not precise. It appears that EXTRA_NO_CONNECTIVITY, at least under some circumstances, is only added to the Intent extras IF there is no connectivity. So accessing it with getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false) is supposed to yield a boolean which is true if there is no connectivity at all, and false if there is any connectivity. Besides, the code in this answer is an **anti pattern** because the analysis is limited. It will not detect Bluetooth tethering, USB tethering or LAN connectivity. Don't do that. – class stacker Dec 17 '12 at 09:40
  • 2
    Attribution for this code, seeing as Ravi forgot it: http://stackoverflow.com/a/4239410/182653 – paulw1128 May 22 '13 at 17:48
  • Why not just get the `getActiveNetworkInfo()` and check if it's connected and done with it? – Ε Г И І И О May 27 '13 at 04:35
  • Yes Ε Г И І И О, you are right in case you need not to know about type of connection you are connected to. – Ravi K. Sharma May 27 '13 at 06:10