Nauman Zubair
Nauman Zubair

Reputation: 1206

Checking Internet connectivity on android

I want to display a message for user if he is out of internet availability. I am using the following snippet of code but its showing exception even in the case of network availability.

What I am doing wrong?

public boolean isInternetAvailable(){

         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnectedOrConnecting()) {
                return true;
            }
            return false;

}

Upvotes: 0

Views: 280

Answers (5)

Sunny
Sunny

Reputation: 7624

I would just like to add that the code posted by OP will only check if there is a network connection. In case of a wifi connection it will just check if the phone is connected to the respective Wifi.

But it will not give u any error in case the wifi actually has an working internet connection.

You might want to use another check in addition to the above, something like this:

try {
                        HttpURLConnection httpConnection = (HttpURLConnection) (new URL("http://clients3.google.com/generate_204").openConnection());
                        httpConnection.setRequestProperty("User-Agent", "Test");
                        httpConnection.setRequestProperty("Connection", "close");
                        httpConnection.setConnectTimeout(15000); 
                        httpConnection.connect();
                        if (httpConnection.getResponseCode() == 204){
                           //internet is avialable    
                            return;
                        }else{
                             Log.e(TAG, "Internet connection error: " + httpConnection.getResponseCode()
                                     + ": " + httpConnection.getResponseMessage());
                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Internet connection error: " + e);
                    }

Upvotes: 0

dmaxi
dmaxi

Reputation: 3297

If you have the required permissions in the Manifest and you have still problem I would modify slightly your code:

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm != null) {
  NetworkInfo netInfo = cm.getActiveNetworkInfo();
  if (netInfo != null && netInfo.isConnected()) {
    return true;
  }
}
return false;

Upvotes: 0

Muhammad Nabeel Arif
Muhammad Nabeel Arif

Reputation: 19310

Add permissions in manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Below is a method which will give you whether you are connected to any network or wifi

public static boolean isNetworkAvailable(Context context) {
        boolean available = false;
        try {

            ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            if (connectivity != null) {
                NetworkInfo[] info = connectivity.getAllNetworkInfo();
                if (info != null) {
                    for (int i = 0; i < info.length; i++) {
                        if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                            available = true;
                        }
                    }
                }
            }
            if (available == false) {
                NetworkInfo wiMax = connectivity.getNetworkInfo(6);

                if (wiMax != null && wiMax.isConnected()) {
                    available = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return available;
    }

Upvotes: 0

Zakaria
Zakaria

Reputation: 15070

It seems that you forget to add this snippet in your manifest file :

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Upvotes: 1

Alexei
Alexei

Reputation: 1068

Which exception do you get? Perhaps you forgot to add the android.permission.ACCESS_NETWORK_STATE in the manifest file.

Upvotes: 1

Related Questions