【问题标题】:Checking Internet connectivity on android检查android上的互联网连接
【发布时间】:2012-02-21 06:50:15
【问题描述】:

如果用户无法上网,我想向他显示一条消息。我正在使用以下 sn-p 代码,但即使在网络可用的情况下也会显示异常。

我做错了什么?

public boolean isInternetAvailable(){

         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnectedOrConnecting()) {
                return true;
            }
            return false;

}

【问题讨论】:

  • 为什么不发送数据包,并处理异常?
  • 你是否在清单中添加了这个:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

标签: java android


【解决方案1】:

你得到什么例外?也许您忘记在清单文件中添加android.permission.ACCESS_NETWORK_STATE

【讨论】:

    【解决方案2】:

    您似乎忘记在清单文件中添加此 sn-p :

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    【讨论】:

      【解决方案3】:

      在清单中添加权限

      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
      

      下面是一个方法,可以让你知道你是否连接到任何网络或 wifi

      public static boolean isNetworkAvailable(Context context) {
              boolean available = false;
              try {
      
                  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) {
                                  available = true;
                              }
                          }
                      }
                  }
                  if (available == false) {
                      NetworkInfo wiMax = connectivity.getNetworkInfo(6);
      
                      if (wiMax != null && wiMax.isConnected()) {
                          available = true;
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
      
              return available;
          }
      

      【讨论】:

        【解决方案4】:

        如果您在 Manifest 中拥有所需的权限,但仍有问题,我会稍微修改您的代码:

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if(cm != null) {
          NetworkInfo netInfo = cm.getActiveNetworkInfo();
          if (netInfo != null && netInfo.isConnected()) {
            return true;
          }
        }
        return false;
        

        【讨论】:

          【解决方案5】:

          我想补充一点,OP 发布的代码只会检查是否有网络连接。 如果有 wifi 连接,它只会检查手机是否连接到相应的 Wifi。

          但如果 wifi 确实有工作的互联网连接,它不会给你任何错误。

          除了上述之外,您可能还想使用其他检查,如下所示:

          try {
                                  HttpURLConnection httpConnection = (HttpURLConnection) (new URL("http://clients3.google.com/generate_204").openConnection());
                                  httpConnection.setRequestProperty("User-Agent", "Test");
                                  httpConnection.setRequestProperty("Connection", "close");
                                  httpConnection.setConnectTimeout(15000); 
                                  httpConnection.connect();
                                  if (httpConnection.getResponseCode() == 204){
                                     //internet is avialable    
                                      return;
                                  }else{
                                       Log.e(TAG, "Internet connection error: " + httpConnection.getResponseCode()
                                               + ": " + httpConnection.getResponseMessage());
                                  }
                              } catch (IOException e) {
                                  Log.e(TAG, "Internet connection error: " + e);
                              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-06-12
            • 1970-01-01
            • 2016-08-28
            • 2016-01-16
            • 2017-11-30
            • 2013-02-02
            • 2011-02-14
            • 2012-03-23
            相关资源
            最近更新 更多