您使用的代码只是用来检查您是否连接到无线网络。它不检查该WiFi是否缓慢。 (没有互联网意味着连接速度慢)。
我试过使用这段代码。在这里,我尝试点击google.com并设置连接超时值。 如果网速很好,那么返回的结果是200.所以我检查结果代码是否为200。如果没有,我会显示一个警报,说明互联网连接速度很慢。在asyntask中使用它,onPostExecute()检查返回结果的值。
HttpURLConnection urlc = null;
try {
urlc = (HttpURLConnection) (new URL("http://www.google.com")
.openConnection());
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000); // choose your own timeframe
urlc.setReadTimeout(2000); // choose your own timeframe
try {
urlc.connect();
// returning connection code.
return (urlc.getResponseCode());
} catch (IOException e1) {
e1.printStackTrace();
}
Kaveesh Kanwal,这不是他要求的。 你给出的代码片段只是为了检查设备是否连接到无线网络。它不测试网速。 –
OP没有询问如何检查网速。祝你好运。 –
他也没有询问如何检查设备是否连接到无线网络。 :) 由于没有互联网接入,我们所能做的只是检查数据包的发送速度。 –