6

In my application I have to get notified whenever the device connects or disconnects from a WIFI network. For this I have to use a BroadcastReceiver but after reading through different articles and questions here on SO I'm a bit confused which Broadcast action I should use for this. In my opinion I have three choices:

To reduce resources I really only want to get notified whenever the device is CONNECTED to a WIFI network (and it has received an IP address) or when the device has DISCONNECTED from one. I do not care about the other states like CONNECTING etc.

So what do you think is the best Broadcast action I should use for this? And do I have to manully filter the events (because I receieve more then CONNECTED and DISCONNECTED) in onReceive?

EDIT: As I pointed out in a comment below I think SUPPLICANT_CONNECTION_CHANGE_ACTION would be the best choice for me but it is never fired or received by my application. Others have the same problem with this broadcast but a real solution for this is never proposed (in fact other broadcasts are used). Any ideas for this?

Cilenco
  • 6,951
  • 17
  • 72
  • 152
  • Where is the ambiguity? Android documentation for SUPPLICANT_CONNECTION_CHANGE_ACTION clearly states "Broadcast intent action indicating that a connection to the supplicant has been established (and it is now possible to perform Wi-Fi operations) or the connection to the supplicant has been lost. One extra provides the connection state as a boolean, where true means CONNECTED." The rest of the intents have extras which can tell you the Wifi State or the complete NetworkInfo object carrying the changed network info. I think it is pretty forward to use (1) given your requirements. – Rahul Shukla Nov 07 '17 at 16:26
  • You are right but the problem is that `SUPPLICANT_CONNECTION_CHANGE_ACTION ` is never received by my application and as I read in other questions this is a common problem. Not all phones are firing this broadcast as far as i can tell. – Cilenco Nov 09 '17 at 06:48
  • Then you should undoubtedly use either of the remaining two broadcasts if they work for you. – Rahul Shukla Nov 09 '17 at 08:16

2 Answers2

2

You can go for WifiManager.NETWORK_STATE_CHANGED_ACTION works.

Register receiver with WifiManager.NETWORK_STATE_CHANGED_ACTION Action, either in Manifest or Fragment or Activity, which ever suited for you.

Override receiver :

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

    final String action = intent.getAction();

    if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
        NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        boolean connected = info.isConnected();
        if (connected)
        //call your method
    }      
}
Lalit Jadav
  • 1,409
  • 1
  • 15
  • 38
1

Please try

@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter();
    filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    filter.addAction("android.net.wifi.STATE_CHANGE");
    registerReceiver(networkChangeReceiver, filter);
}

@Override
protected void onDestroy() {
    unregisterReceiver(networkChangeReceiver);
    super.onDestroy();
}

and

BroadcastReceiver networkChangeReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (!AppUtils.hasNetworkConnection(context)) {
            showSnackBarToast(getNetworkErrorMessage());
        }

    }
};

I am using this and it is working for me. Hope it will help you out.

Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30
  • Thanks but I know how to do this. The real question is if this `IntentFilter` (the used action in it) is the best solution to only get connect and disconnect events. – Cilenco Sep 02 '17 at 14:04