我正在android中开发一个从数据库中获取数据的应用程序 . 我已经开发了代码来查找WIFI或Mobile数据是打开还是关闭,如下所示:
protected boolean isNetworkAvailable()
{
boolean connection=true;
ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] activeNetworkInfo=connectivityManager.getAllNetworkInfo();
for(NetworkInfo ni:activeNetworkInfo)
{
if(ni.getTypeName().equalsIgnoreCase("WIFI"))
if(ni.isConnected())
haveConnectedWifi=true;
if(ni.getTypeName().equalsIgnoreCase("MOBILE"))
if(ni.isConnected())
haveConnectedMobile=true;
}
if(haveConnectedWifi==false && haveConnectedMobile==false)
{
connection=false;
}
return connection;
}
现在假设我的WIFI或移动数据网络已打开,但我根本没有连接到互联网(WIFI没有连接到任何接入点) . 如何在Android中显示这个?
谢谢,
7 回答
你可以简单地使用这个:
这个课程非常完整:
试试这个,
此代码可能对您有所帮助 .
试试这个:
资料来源:Check for Active internet connection Android
调用此类的isConnectingToInternet函数来检查您想要检查Internet连接的Internet连接 .
To ensure that actual internet is available or not need to ping any global url e.g www.google.com
This will work 100%