【发布时间】:2016-05-11 12:06:21
【问题描述】:
public boolean isConnected(Context context) {
connected=false;
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.getState() == NetworkInfo.State.CONNECTED && info.isConnected()) {
Log.e("link",new Gson().toJson(info.getDetailedState()));
connected=true;
Needle.onBackgroundThread().execute(new Runnable() {
@Override
public void run() {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL(mainDomain).openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
connected=true;
Log.e("connection",new Gson().toJson(urlc.getResponseCode()));
} catch (IOException e) {
e.printStackTrace();
connected=false;
}
}
});
return connected;
}
}
return connected;
}
我已经使用了这个代码。它工作正常,但我的问题是我需要调用服务器。有没有其他方法可以检查 WiFi 的速度或工作情况?
WifiManager wifiManager=(WifiManager)this.getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int speedMbps = wifiInfo.getLinkSpeed();
我使用上面的代码来检查速度,但它也不能正常工作。
请帮帮我。提前致谢
【问题讨论】:
标签: android networking connection android-wifi