如何检查Android中的Internet连接以进行wifi连接?

kar*_*hik 5 android

当wifi连接到无线调制解调器互联网覆盖是否存在它总是说是你连接它只检查wifi连接而不是互联网连接所以如何处理这种情况?

Chi*_*ani 3

1.)你可以检查它

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

或者

2.)你可以做一些像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
    }
Run Code Online (Sandbox Code Playgroud)