Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast action for WIFI change

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:

  • SUPPLICANT_CONNECTION_CHANGE_ACTION
  • NETWORK_STATE_CHANGED_ACTION
  • CONNECTIVITY_ACTION

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?

like image 528
Cilenco Avatar asked Sep 02 '17 08:09

Cilenco


People also ask

What is broadcast on Android?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

How to disable broadcast receiver in Android?

To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.

What is protected broadcast in Android?

To prevent arbitrary processes from sending sensitive broadcast Intents, Android allows the declaration of sensitive broadcast actions as “protected” by using the 'protected-broadcast' element in an authorized app's AndroidManifest.


2 Answers

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
    }      
}
like image 183
Lalit Jadav Avatar answered Oct 10 '22 09:10

Lalit Jadav


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.

like image 22
Rahul Sharma Avatar answered Oct 10 '22 10:10

Rahul Sharma