Intent action for network events in android sdk
Asked Answered
M

2

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.

Murtha answered 19/2, 2010 at 8:44 Comment(0)
C
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());
    }

}
Clump answered 19/2, 2010 at 9:3 Comment(5)
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?Melissamelisse
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).Redouble
The answer looks the same as the the question. So why did it NOT work before the answer?Odine
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.Shakti
This code works, but it only tells you "connectivity change" is there a way to tell if it's connected or dissconnected?Bergamot
T
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;    
    }
}
Transaction answered 5/10, 2012 at 11:39 Comment(5)
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.Repp
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.Repp
Attribution for this code, seeing as Ravi forgot it: https://mcmap.net/q/11338/-detect-whether-there-is-an-internet-connection-available-on-android-duplicateBradski
Why not just get the getActiveNetworkInfo() and check if it's connected and done with it?Aphasia
Yes Ε Г И І И О, you are right in case you need not to know about type of connection you are connected to.Transaction

© 2022 - 2024 — McMap. All rights reserved.