验证设备是否与Internet连接,而不仅仅是网络连接

时间:2016-05-05 17:50:57

标签: android wifi connectivity

我需要一些方法来验证我的网络是否被连接到interne,在我的研究中我发现了一些方法描述如下:

public static boolean hasInternetAccess(Context context) {
    if (isNetworkAvailable(context)) {
        try {
            HttpURLConnection urlc = (HttpURLConnection) 
                (new URL("http://clients3.google.com/generate_204")
                .openConnection());
            urlc.setRequestProperty("User-Agent", "Android");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(1500); 
            urlc.connect();
            return (urlc.getResponseCode() == 204 &&
                        urlc.getContentLength() == 0);
        } catch (IOException e) {
            Log.e(TAG, "Error checking internet connection", e);
        }
    } else {
        Log.d(TAG, "No network available!");
    }
    return false;
}

...但我的节目将在中国发布,谷歌在中国被封锁。

我想创建一个像这样的方法首先选择我的本地化来获得一个Baidoo网站 - 比如谷歌在中国 - 但是这是一个非常广泛的方法来检查设备是否在某个网络上连接并验证是否连接在互联网上。

3 个答案:

答案 0 :(得分:1)

你可以使用ConnectivityManager尝试这样的事情:

public static boolean  isConnectedToInternet(Context context){
        boolean isConnected= false;
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);        
        isConnected= (wifi.isAvailable() && wifi.isConnectedOrConnecting() || (mobile.isAvailable() && mobile.isConnectedOrConnecting()));
        return isConnected;
    }

您还可以查看所选答案herehere

答案 1 :(得分:0)

使用Inet地址检查连接是否可用,使用以下代码。

  

示例代码

public static void isNetworkAvailable(Context context){
    HttpGet httpGet = new HttpGet("http://www.yahoo.com");
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    try{
        Log.d(TAG, "Checking network connection...");
        httpClient.execute(httpGet);
        Log.d(TAG, "Connection OK");
        return;
    }
    catch(ClientProtocolException e){
        e.printStackTrace();
    }
    catch(IOException e){
        e.printStackTrace();
    }

    Log.d(TAG, "Connection unavailable");
}

答案 2 :(得分:0)

China does有托管Android应用的网站。中国有proxy servers你可以用它来看看被封锁的东西,可以用来挫败中国的防火墙。我强烈建议您考虑法律后果。 TCP / IP实用程序甚至浏览器都可用于测试连接。我不确定我完全理解你的问题。您有Android应用程序将用于位于中国的设备和位于您想要测试连接的服务器。如果你想走那条路,中国有web hosting。您是否需要访问Google API?