![](/img/trans.png)
[英]How to check Wifi is connected, but no Internet access in Android
[英]How check wifi router accessing internet or not In Android Program? Mobile is connected to wifi router but router not accessing internet?
我可以检查手机是否已连接到 wifi 路由器,但如何检查路由器是否访问互联网?如果手机已连接到路由器,则返回 true 但不检查互联网访问请帮助...
public boolean isInternetAvailable() {
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
{
for (int i = 0; i < info.length; i++)
// if (info[i].(info[i].getState() == NetworkInfo.State.CONNECTED)
if(info[i]!=null && info[i].isAvailable() && info[i].isConnected())
{
try {
if (InetAddress.getByName("https://www.google.co.in").isReachable(3000))
{
return true;
}
else
{
return false;
// host not reachable
}
} catch (IOException e) {
e.printStackTrace();
}
Log.i("Network Info", info[i].toString());
return true;
}
}
return false;
}
return false;
}
}
使用此功能。 它将检查互联网连接的实际状态是否正在运行。
注意此功能将 ping 到 google 公共 DNS 服务器以检查连接。 所以它不能保证在 DNS 服务器关闭的情况下给出正确的结果。 虽然谷歌 DNS 服务器几乎每次都在线,所以我们可以信任它。
public boolean isOnline() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException e){
e.printStackTrace();
} catch (InterruptedException e){
e.printStackTrace();
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.