How to check the internet connection in Android for Wi-Fi connection?

When Wi-Fi is connected to a wireless modem, is there Internet coverage or not, it always says yes, you are connected, it only checks Wi-Fi compatibility, not the Internet connection, and how to deal with this situation?

+5
source share
7 answers

1.) u can check it like

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 something 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
    }
+3
source

Try it.

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 this helps.

+2
source

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

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


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

?

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

:

Indicates if a network connection exists and you can establish connections and transfer data.

0
source

That's right, if you use ConnectivityManager, specify only connection information (WiFi, mobile, WiMax, etc.), and if it is connected or not.

To provide data connectivity, you can 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;
}
0
source

You can use this to check your Android internet connection for Wi-Fi or mobile data.

 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;
    }
0
source

Just like that

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

All Articles