如何检测WIFI何时连接到互联网?

时间:2011-08-24 02:48:53

标签: android networking wifi

我正在构建一个Android应用程序,我使用下面的代码来检测是否存在网络连接。它运行良好,可以检测移动和WIFI网络。

我的问题是如何检测实际的互联网连接。连接到WIFI时,以下代码返回true,但WIFI可能不一定连接到Internet。

代码

    protected boolean checkInternetConnection() {     
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
    // test for connection     
    if (cm.getActiveNetworkInfo() != null             
            && cm.getActiveNetworkInfo().isAvailable()             
            && cm.getActiveNetworkInfo().isConnected()) {

        return true;

        } 
    else {               
        return false;

        } 

} //end checkInterneConnection method

感谢您的时间。

梅尔

5 个答案:

答案 0 :(得分:1)

您应该尝试访问互联网地址。因此,您应该检查InetAddress类和方法isReachable:http://developer.android.com/reference/java/net/InetAddress.html#isReachable%28int%29

答案 1 :(得分:1)

这段代码会检查你的设备是否有互联网连接,如果信号差,它会显示Toast其他明智的不是,

ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo Info = conMan.getActiveNetworkInfo();

        if(Info == null){
            Toast.makeText(RegisterActivity.this,"Network Connection Failed! ", Toast.LENGTH_SHORT).show();
        }

答案 2 :(得分:0)

您可以尝试ping http://google.com或执行此类操作以确认可以访问互联网。

答案 3 :(得分:0)

你应该试试这个:

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) 
            m_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)
                  {
                      return true;
                  }
      }
      return false;
}

只检查wifi更简单:

private boolean isWifiConnected() {
    int WIFI_STATE = wifi.getWifiState();
    if(WIFI_STATE == WifiManager.WIFI_STATE_ENABLED)
        return true;

    return false;
}

答案 4 :(得分:0)

此代码将真正测试互联网连接:

                    final DeviceOrientation exifDerivedRotation;
                if (exifOrientation == null) {
                    // No existing rotation value is assumed to be 0
                    // rotation.
                    exifDerivedRotation = DeviceOrientation.CLOCKWISE_90;
                } else {
                    //exifDerivedRotation = DeviceOrientation
                      //      .from(exifOrientation);
                    exifDerivedRotation = DeviceOrientation.CLOCKWISE_90;
                }

    // Resulting image will be rotated so that viewers won't
    // have to rotate. That's why the resulting image will have 0
    // rotation.
            resultImage = new TaskImage(
            DeviceOrientation.CLOCKWISE_90, resultSize.getWidth(),
                      resultSize.getHeight(),
                      ImageFormat.JPEG, null);
    // Image rotation is already encoded into the bytes.