Broadcast receiver not detecting lost wifi connection
android, broadcastreceiver, wifi
Solution
Update: I've found the problem, so I'll answer this myself. I'm using exclusively SUPPLICANT_STATE_CHANGED_ACTION because it is checking for ANY wifi status--disconnected, disabled, connected, enabled, etc. The other one is pretty useless for monitoring a connection, as it only checks if the user has the wifi enabled in Settings or not.
Registering my receiver (in onResume):
IntentFilter filter = new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
this.registerReceiver(wifiStatusReceiver, filter);
My new code looks like this:
BroadcastReceiver wifiStatusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
logger.d("checking wifi state...");
SupplicantState supState;
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();
logger.d("supplicant state: " + supState);
SherlockDialogFragment dialog = (SherlockDialogFragment) fragmentManager
.findFragmentByTag(WifiAlertDialogFragment.DIALOG_WIFI);
if (supState.equals(SupplicantState.COMPLETED)) {
logger.d("wifi enabled and connected");
if (dialog != null)
dialog.dismiss();
} else {
WifiAlertDialogFragment.wifiCheck(HomeActivity.this);
if (supState.equals(SupplicantState.SCANNING)) {
logger.d("wifi scanning");
} else if (supState.equals(SupplicantState.DISCONNECTED)) {
logger.d("wifi disonnected");
} else {
Toast.makeText(HomeActivity.this, "Wifi Enabling",
Toast.LENGTH_LONG).show();
logger.d("wifi connecting");
}
}
}
};
Problem
I'm having some trouble detecting a change in wifi. I'd like my app to monitor the wifi network and create an alert dialog if the wifi is disconnected a. on startup, resume, etc. or b. anytime the connection is lost. Here is my code (I'm registering it in onResume with this.registerReceiver(wifiStatusReceiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)); and unregistering it in onPause): ``` BroadcastReceiver wifiStatusReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int extraWifiState = intent.getIntExtra( WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN); final String action = intent.getAction(); DialogFragment dialog = (DialogFragment) fragmentManager .findFragmentByTag(WifiAlertDialogFragment.DIALOG_WIFI); if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) { NetworkInfo info = (NetworkInfo) intent .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); if (info.getState().equals(NetworkInfo.State.CONNECTED)) { logger.d("wifi changed, is still connected"); if (dialog != null) dialog.dismiss(); } else { logger.d("connection lost"); if (dialog == null) WifiAlertDialogFragment.wifiCheck(HomeActivity.this); } } switch (extraWifiState) { case WifiManager.WIFI_STATE_DISABLED: WifiAlertDialogFragment.wifiCheck(HomeActivity.this); break; case WifiManager.WIFI_STATE_DISABLING: Toast.makeText(HomeActivity.this, R.string.wifi_disabled, Toast.LENGTH_SHORT).show(); WifiAlertDialogFragment.wifiCheck(HomeActivity.this); break; case WifiManager.WIFI_STATE_ENABLED: if (dialog != null) dialog.dismiss(); break; case WifiManager.WIFI_STATE_ENABLING: if (dialog != null) dialog.dismiss(); Toast.makeText(HomeActivity.this, "Wifi Enabling", Toast.LENGTH_LONG).show(); break; case WifiManager.WIFI_STATE_UNKNOWN: // no op break; } } }; ```