I am using this BroadcastReceiver to monitor if the WIFI is connected or disconnected , but it is not working for me, it detects that the WIFI is connected but not when it is disconnected, can someone tell me what is the error?
public class MainActivity extends Activity {
private BroadcastReceiver WifiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int WifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
switch (WifiState) {
case WifiManager.WIFI_STATE_ENABLED: {
Toast.makeText(context,"Wifi enabled",Toast.LENGTH_LONG).show();
}
break;
case WifiManager.WIFI_STATE_ENABLING: {
Toast.makeText(context,"Wifi enabling",Toast.LENGTH_LONG).show();
}
break;
case WifiManager.WIFI_STATE_DISABLED: {
Toast.makeText(context,"Wifi disabled",Toast.LENGTH_LONG).show();
}
break;
case WifiManager.WIFI_STATE_DISABLING: {
}
break;
case WifiManager.WIFI_STATE_UNKNOWN: {
}
break;
}
} };
protected void onResume(){
super.onResume();
registerReceiver(WifiReceiver, new IntentFilter( WifiManager.WIFI_STATE_CHANGED_ACTION));
}
protected void onPause(){
super.onPause();
unregisterReceiver(WifiReceiver);
}
}
You can get when there is no connection by:
According to the above this is an example class:
Don't forget to register your
BroadcastReceiver
: