mohan
mohan

Reputation: 13165

How to give notification to the user if a wifi network is not available in android

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

Answers (2)

Kannan Suresh
Kannan Suresh

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

Reno
Reno

Reputation: 33792

Here is a tutorial to do this, it can do these things:

  • Is WiFi available on the device
  • Is the WiFi enabled Is the WiFi
  • Connected to an access point

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

Related Questions