4

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

    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 已连接..如果我使用以上代码,它只会检查 wifi 是否已连接或断开.. 如果有人给出,将不胜感激我一个解决方案...

4

6 回答 6

3

最简单的事情是发送 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();
  }
于 2013-01-22T06:22:27.410 回答
1

是的,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.
}
于 2013-01-22T08:11:50.043 回答
1

刚刚在 Play 商店 https://play.google.com/store/apps/details?id=com.mobilefree上完成了最新标题“Mobile Free”的工作

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

物理网络可以是 WIFI、MOBILE、ETHER 等……传输层。

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

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

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

isReachable() 方法使用 ICMP 来 ping 主机。这可能很难设置。

同样,根据设备、权限、API、操作系统版本等,调用 url 可以同样受到限制。

如果您不必这样做,请不要这样做。没有适用于所有设备的可靠方法。

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

有趣的是,您实际上可以在移动设备上,但没有互联网并且仍然可以计费。

于 2014-01-27T02:38:16.717 回答
0

非常感谢你给我的建议...

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

我使用了 try\catch:

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

    e.printStackTrace();

    Connection_error();     
    }

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

谢谢大家...

于 2013-01-22T09:35:25.970 回答
0

您可以读取wifi当前状态..

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

也看看这个方法

于 2015-07-08T10:33:03.607 回答
-1

试试下面的代码: -

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;
    }
}
于 2013-01-22T07:05:55.993 回答