Reputation: 13165
Can anybody tell me how to give a notification to the user if a wifi network is not available in android, like broad cast receiver in android? Can anybody provide an example?
Thanks
Upvotes: 1
Views: 244
Reputation: 4580
You may use the following code:
public final boolean isWiFiOn() {
ConnectivityManager connec = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if ((connec != null) && ((connec.getNetworkInfo(1) != null)
&& (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING
|| connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED || connec
.getNetworkInfo(1).getState() == NetworkInfo.State.SUSPENDED)) {// To
// check
// WiFi
// connection
return true;
} else {
return false;
}
}
Hope this would help.
Upvotes: 1
Reputation: 33792
Here is a tutorial to do this, it can do these things:
The gist of it is :
private class ConnectionChangeReceiver extends BroadcastReceiver {
public void onReceive( Context context, Intent intent ) {
Log.d(tag, "Inside Broadcast Reciever");
CheckWifiStatus();
}
}
private void RegisterWifiWatcher()
{
if(wifiWatcher == null)
wifiWatcher = new ConnectionChangeReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(wifiWatcher, intentFilter);
}
Upvotes: 3