繁体   English   中英

如何检查互联网是否可用wifi(虽然连接了Wifi)

[英]How to check internet is available in wifi(Though Wifi is connected )

我使用此代码检查互联网是否可用..

    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已连接..如果我使用abouve代码,它只是检查wifi是连接还是断开..如果有些给出它会很感激我的解决方案......

最简单的方法是发送http请求,如果你的http请求超时,那么你没有互联网连接,或者你可以检查更多的响应代码。 当您连接到WiFi但WiFi路由器没有与ISP的活动连接时,会出现您提到的情况。 最好的事情是依赖http请求imo。

这是示例代码:

try
  {

   HttpGet request = new HttpGet(url));
   HttpParams httpParameters = new BasicHttpParams();
   // Set the timeout in milliseconds until a connection is established.
   // The default value is zero, that means the timeout is not used. 
   int timeoutConnection = 60000;
   HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
   // Set the default socket timeout (SO_TIMEOUT) 
   // in milliseconds which is the timeout for waiting for data.
   int timeoutSocket = 60000;
   HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
   // create object of DefaultHttpClient    
   DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
   request.addHeader("Content-Type", "application/json");
   HttpResponse response = httpClient.execute(request);
   // get response entity
   HttpEntity entity = response.getEntity();
   // convert entity response to string

     if (entity != null)
      {
         result = EntityUtils.toString(entity);
      }

   }
 catch (SocketException e)
  {
     return "-222" + e.toString();
  }
 catch (Exception e)
  {
     return "-333" + e.toString();
  }

是VendettaDroid是对的。

此处还有检查WIFI上网的代码并检查航班模式是否开启,如果您正在制作网络应用程序,则必须处理航班模式

// 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.
}

刚刚在Play商店中使用此最新标题“Mobile Free” https://play.google.com/store/apps/details?id=com.mobilefree

NetworkInfo.isAvailable()只是告诉您是否有可连接的物理网络。 NetworkInfo.isConnected()只是告诉您已连接到物理网络。

物理网络可以是WIFI,MOBILE,ETHER等......传输层。

仅仅因为您连接到网络并不意味着网络具有互联网(应用层)。

网络可以拥有各种应用协议。 TCP,DNS和HTTP不是唯一的。

在android中检查实际的互联网可用性实际上非常困难。 要做到这一点,有许多限制要克服。

isReachable()方法使用ICMP ping主机。 这可能很难建立。

同样,调用URL也可以受到同等限制,具体取决于设备,权限,API,操作系统版本等。

如果你不必这样做,不要。 没有强大的方法可以在所有设备上运行。

这不是“移动免费”的核心用例,它只管理移动计费(开/关)。

有趣的是,你可以实际上是在手机上,但没有互联网,仍然可以收费。

非常感谢建议我......

我其实找到了一些解决方案:

我用过try \\ catch:

    try{
    //calling url
    }
    catch(UnknownHostException e){

    e.printStackTrace();

    Connection_error();     
    }

   catch (ConnectException e) {
    e.printStackTrace();
    Connection_error();
            }

谢谢大家...

你可以看看wifi当前状态..

SupplicantState supState; 
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();

还看看这种方法

尝试以下代码: -

public static boolean netConnect(Context ctx) {

    ConnectivityManager cm;
    NetworkInfo info = null;
    try {
        cm = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        info = cm.getActiveNetworkInfo();
    } catch (Exception e) {

        e.printStackTrace();
    }
    if (info != null) {
        return true;

    } else {
        return false;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM