【问题标题】:Know if google glass has internet connectivity without waiting TIME_OUT无需等待 TIME_OUT 即可了解 Google Glass 是否有互联网连接
【发布时间】:2014-07-01 14:25:00
【问题描述】:

我需要知道我的眼镜是否有互联网连接。

我已经尝试使用这个解决方案How to check if Google Glass is connected to internet using GDK

public static void isNetworkAvailable(Context context){
HttpGet httpGet = new HttpGet("http://www.google.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");
}

我在 XE 17.1 中可以正常工作,但是当我将 Glass 更新到 XE 18.1 时它可以工作,但我需要等到 TIME_OUT 才能知道 Glass 没有互联网连接。在 XE 17.1 中它不会发生。如果我没有连接,则在不等待 TIME_OUT 的情况下引发异常。

知道无需等待超时即可知道互联网连接的任何想法?

谢谢!

【问题讨论】:

    标签: android google-glass google-gdk


    【解决方案1】:

    您是否尝试过使用InetAddress.getByName("google.com").isReachable(<timeout>) 来测试连接性?它可能不如通过 TCP 的 GET 请求可靠,但我已经看到它在实践中运行良好。

    【讨论】:

    • 嗨!是的,我尝试过,但是当我使用 InetAddress.getByName("google.com").isReachable(<timeout>) 时,有时它对我不起作用,当我没有 Internet 连接时,我得到 true >。
    猜你喜欢
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多