2013-01-22 165 views
3

我用这个代码来检查网络是否可用不可用..如何检查网络在WiFi(无线上网虽然连接)

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 eventhough无线连接..如果我使用abouve代码它只是检查无线连接或断开..它将是感恩,如果有人给我一个解决方案...

+0

顺便说一句,你可以通过轻松使用谷歌找到答案。在stackoverflow中也有很多类似的问题。 – VendettaDroid

回答

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(); 
    } 
+0

谢谢VendettaDroid:让我检查这个.. –

+0

如果你找到你的答案。只要接受它,它会让你接受比例。 – VendettaDroid

-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; 
    } 
} 
+0

HCD:其实Wifi已连接...所以此代码将返回true。如果没有互联网连接到WiFi,但WiFi连接到device.device juz检查无线连接与否。 –

1

是VendettaDroid是正确的。

也在这里是代码来检查无线网络连接和检查飞行模式的ON/OFF,如果你是一个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 
} 

检查无线网络连接和网络可用性

ConnectivityManager cm; 
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
if (cm.getActiveNetworkInfo() != null 
       && cm.getActiveNetworkInfo().isAvailable() 
       && cm.getActiveNetworkInfo().isConnected()) { 

// Internet is availble. 
} 
0

多谢提示我...

我居然发现了一些solutios:

我曾经尝试\美中不足的是:在Play商店 https://play.google.com/store/apps/details?id=com.mobilefree

NetworkInfo.isAvailable

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

    e.printStackTrace(); 

    Connection_error();  
    } 

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

感谢所有...

1

通过这只是工作了最新的标题 “手机免费”( )只是告诉你是否有物理网络可供连接。 NetworkInfo.isConnected()只会告诉您您已连接到物理网络。

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

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

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

检查实际的互联网可用性实际上是相当困难的机器人。 要做到这一点,有许多限制要克服。

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

同样,调用一个URL可以是同样的限制,根据设备,权限,API,操作系统版本等

如果你没有这样做,不上。没有可靠的方法可以在所有设备上使用。

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

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

0

您可以阅读wifi current state..

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

也看看this approach

相关问题