1

I'd like to notify my Activity of any Wifi connection changes using the BroadcastReceiver. Since this broadcast is within the application I'm trying to use the more efficient LocalBroadcastManager object.

However no matter what I do, the BroadcastReceiver.onReceive() method will not fire. I may have wired it up incorrectly, or perhaps the WifiManager.NETWORK_STATE_CHANGED_ACTION action I'm listening for cannot be registered against a LocalBroadcastManager? Any help or clarification would be appreciated.

Here's a sample of my Activity class which contains all the logic.

public class MyActivity extends ActionBarActivity {

private BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
        {
            // Do something
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    IntentFilter wifiStatusIntentFilter = new IntentFilter();
    wifiStatusIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    wifiStatusIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    LocalBroadcastManager.getInstance(this).registerReceiver(wifiReceiver, wifiStatusIntentFilter);
}

protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(wifiReceiver);
}

protected void onResume() {
    super.onResume();

    IntentFilter wifiStatusIntentFilter = new IntentFilter();
    wifiStatusIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    LocalBroadcastManager.getInstance(this).registerReceiver(wifiReceiver, wifiStatusIntentFilter);
}

}

When I switch the wifi on my mobile on and off, or enter and leave the wifi range, the onReceive() method is never fired.

Nathan Liu
  • 2,135
  • 2
  • 14
  • 7

2 Answers2

1

Since this broadcast is within the application I'm trying to use the more efficient LocalBroadcastManager object.

That only works for broadcasts that you send via LocalBroadcastManager. It does not work for system broadcasts, particularly those sent by other processes.

perhaps the WifiManager.NETWORK_STATE_CHANGED_ACTION action I'm listening for cannot be registered against a LocalBroadcastManager?

Correct.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

You can't receive WifiManager.NETWORK_STATE_CHANGED_ACTION with LocalBroadcastManager. LocalBroadcastManager works only within your process.

Helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with sendBroadcast(Intent):

  • You know that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.
  • It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes they can exploit.
  • It is more efficient than sending a global broadcast through the system.

You should use registerReceiver of Context

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94