我用这个代码来检查互联网是否可用。
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].getState() == NetworkInfo.State.CONNECTED && info[i].isAvailable())
{
return true;
}
}
return false;
但我想要的是:
假设 wifi 已连接,但 wifi 中没有互联网...即使 wifi 已连接,如何检查 wifi 中是否有互联网数据..如果我使用 abouve 代码,它只会检查 wifi 是否已连接或断开连接..如果有人给出,将不胜感激我一个解决方案...
是的,VendettaDroid 是对的。
这里还有检查 WIFI 互联网并检查飞行模式是否打开/关闭的代码,如果您正在制作 Web 应用程序,则必须处理飞行模式
// Check Flight mode ON/OFF
if (Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 0) {
// Flight mode ON not able to access internet
}
检查 wifi 和互联网可用性
ConnectivityManager cm;
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
// Internet is availble.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)