简体   繁体   中英

How to check Internet Connection in Android for wifi connectivity?

当 wifi 连接到无线调制解调器互联网覆盖范围是否存在时,它总是说是您已连接,它只检查 wifi 连接性而不是互联网连接性,那么如何处理这种情况?

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
    }

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.

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);

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.

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

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

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM