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

发布于 2024-12-01 05:50:52 字数 751 浏览 2 评论 0原文

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

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

代码

    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

感谢您的宝贵时间。

梅尔

I am building an Android app and I use the code below to detect whether there is a network connection. It works well and detects both mobile and WIFI networks.

My problem is how to detect an actual internet connection. The code below returns true when connected to WIFI however the WIFI might not necessarily be connected to the Internet.

The code

    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

Thanks for your time.

Mel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

金兰素衣 2024-12-08 05:50:53

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

You should try to reach an internet adress. Therefor you should check the InetAdress class and the method isReachable: http://developer.android.com/reference/java/net/InetAddress.html#isReachable%28int%29

心的位置 2024-12-08 05:50:53

这段代码将检查您的设备是否已连接互联网,如果信号差则会显示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();
        }

This piece of code will check whether your device Internet conecction, If the signal is Poor it will show a Toast other wise not,

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();
        }
晨敛清荷 2024-12-08 05:50:53

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

You can try ping http://google.com or doing something like this to confirm it's ok to visit internet.

凉世弥音 2024-12-08 05:50:53

你应该尝试这个:

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

You should try this:

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

And to check only wifi is simpler:

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

    return false;
}
薄情伤 2024-12-08 05:50:53

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

public static boolean isInternetAvailable() {
   try {
      InetAddress address = InetAddress.getByName("google.com");
      return address.isReachable(2000); //This really tests if the ip, given by the url, is reachable 
      //return !address.equals(""); //This just tests if the IP is available but it could be taken in a previous request when internet was available
   } catch (Exception e) {
     Log.d("Internet check", "Unable to reach the url: "+url);
   }

   return false;
}

This code will really test the internet connection:

public static boolean isInternetAvailable() {
   try {
      InetAddress address = InetAddress.getByName("google.com");
      return address.isReachable(2000); //This really tests if the ip, given by the url, is reachable 
      //return !address.equals(""); //This just tests if the IP is available but it could be taken in a previous request when internet was available
   } catch (Exception e) {
     Log.d("Internet check", "Unable to reach the url: "+url);
   }

   return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文