karthik
karthik

Reputation: 17842

How to check Internet Connection in Android for wifi connectivity?

when wifi is connected to wireless modem internet coverage is there or not it always says yes you are conected it only checks wifi conectivity not internet connectivity so how to handle such a situation ?

Upvotes: 5

Views: 6164

Answers (7)

Aashutosh Kumar
Aashutosh Kumar

Reputation: 9

Use This Code

final ConnectivityManager connectivityManager = (ConnectivityManager)context.
            getSystemService(Context.CONNECTIVITY_SERVICE);

final Network network = connectivityManager.getActiveNetwork();
final NetworkCapabilities capabilities = connectivityManager
            .getNetworkCapabilities(network);

return capabilities != null
                && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);

Upvotes: 1

nicolas asinovich
nicolas asinovich

Reputation: 3511

Also just like this

public static boolean isNetworkAvailable(Context context)
{
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnectedOrConnecting();
}

Upvotes: 0

Selim Raza
Selim Raza

Reputation: 497

You can Use this to check Internet Connection in Android for wifi Or Mobile Data connectivity

 public boolean isConnected() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean NisConnected = activeNetwork != null && activeNetwork.isConnected();
        if (NisConnected) {
            //  if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE || activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
           return true;
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return true;
            else
                return false;
        }
        return false;
    }

Upvotes: 0

Arun Badole
Arun Badole

Reputation: 11097

Try this.

private boolean haveNetworkConnection(Context context)
    {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo)
        {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            {
                if (ni.isConnected())
                {
                    haveConnectedWifi = true;
                    Log.v("WIFI CONNECTION ", "AVAILABLE");
                } else
                {
                    Log.v("WIFI CONNECTION ", "NOT AVAILABLE");
                }
            }
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            {
                if (ni.isConnected())
                {
                    haveConnectedMobile = true;
                    Log.v("MOBILE INTERNET CONNECTION ", "AVAILABLE");
                } else
                {
                    Log.v("MOBILE INTERNET CONNECTION ", "NOT AVAILABLE");
                }
            }
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

Hope it will help.

Upvotes: 1

Manus Reload
Manus Reload

Reputation: 1

Right, if you use ConnectivityManager only give information of the connection (WiFi, mobile, WiMax, etc) and if it is connected or not.

To ensure that a data connection you can make a ping:

public static boolean ping() {

    try {
        SocketAddress addr = new InetSocketAddress("www.example.com", 80); // Set IP/Host and Port
        Socket socket = new Socket();
        //Connect socket to address, and set a time-out to 5 sec
        socket.connect(addr, 5000);
        //If network isn't conecctet then throw a IOException else socket is connected successfully
        socket.close();
        return true;
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

Upvotes: 0

Chintan Raghwani
Chintan Raghwani

Reputation: 3370

1.)u can check it as

URL url = new URL("YOUR urlString");
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
.
.
int responseCode = conn.getResponseCode();
//if responseCode = 200 - THEn CONN is connected

OR

2.) u can do somethin like dis

public static boolean isNetworkAvailable(Activity activity) {
        ConnectivityManager connectivity = (ConnectivityManager) activity
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;  //<--  --  -- Connected
                    }
                }
            }
        }
        return false;  //<--  --  -- NOT Connected
    }

Upvotes: 3

MRD
MRD

Reputation: 7184

Have you tried this one?

http://developer.android.com/reference/android/net/NetworkInfo.html#isConnected()

The description says:

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

Upvotes: 0

Related Questions