【问题标题】:Check network connection android检查网络连接android
【发布时间】:2012-04-18 02:00:08
【问题描述】:

在我在模拟器上测试的应用程序中,我使用以下代码检查网络连接(WIFI):

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

这个方法总是返回true,即使我禁用了我电脑的无线连接... 这是模拟器造成的还是其他原因?

如果这不是检查网络连接的正确方法,我该怎么做?

【问题讨论】:

  • 在飞行模式下设置模拟器后检查此代码
  • 你试过isAvailable()isConnected()吗?
  • @vipin,在飞行中它在两种情况下都返回 false...
  • 是的,亲爱的,当你禁用你的 wifi 移动网络时,你没有关闭,所以连接是真实的,现在在飞行中你没有网络,所以你的代码是正确的

标签: android networking connection


【解决方案1】:

最好 (1) 传入上下文,以便每个活动都可以调用此函数,以及 (2) 将此函数设为静态:

 public boolean isNetworkOnline() {
    boolean status=false;
    try{
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getNetworkInfo(0);
        if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) {
            status= true;
        }else {
            netInfo = cm.getNetworkInfo(1);
            if(netInfo!=null && netInfo.getState()==NetworkInfo.State.CONNECTED)
                status= true;
        }
    }catch(Exception e){
        e.printStackTrace();  
        return false;
    }
    return status;

    }  

【讨论】:

  • 但我需要在模拟器上进行测试...有什么替代方案?
【解决方案2】:

要让 getActiveNetworkInfo() 工作,您需要将以下内容添加到清单中。

1. uses-permission android:name="android.permission.INTERNET"
 2. uses-permission
    android:name="android.permission.ACCESS_NETWORK_STATE"

最好使用 netInfo.isConnected() 而不是 netInfo.isConnectedOrConnecting

也试试这个

Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(ConnectivityManager.TYPE_MOBILE)

Context.getSystemService(Context.CONNECTIVITY_SERVICE).requestRouteToHost(TYPE_WIFI, int hostAddress)

【讨论】:

    【解决方案3】:

    就我而言,我关闭了 Wifi,但忘记关闭数据/移动网络连接。一旦我这样做了,该线程的原始帖子中的代码就对我有用。

    回顾一下:

    1. 进入设置
    2. 关闭无线网络
    3. 进入设置->数据使用
    4. 关闭移动数据

    【讨论】:

      【解决方案4】:

      这就是我将它放在一个项目中的方式:

      清单文件:

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

      静态函数:

      public static boolean isNwConnected(Context context) {
          if (context == null) {
              return true;
          }
          ConnectivityManager connectivityManager = 
              (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo nwInfo = connectivityManager.getActiveNetworkInfo();
          if (nwInfo != null && nwInfo.isConnectedOrConnecting()) {
              return true;
          }
          return false;
      }
      

      【讨论】:

        【解决方案5】:

        我们可以使用以下功能检查互联网连接的连接状态。 Google Android 已更新 Android API 的功能以检查网络状态。现在在 Android 最新的 API 中,getAllNetworkInfo() 已被弃用。但是getAllNetworks() 函数只允许Build.VERSION.SDK_INT &gt;= 21.I 为所有情况编写代码。

        要检查互联网连接,您可以调用此函数。

        public static boolean checkInternetConnection(Context context) {
                ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connectivity == null) {
                return false;
            } else if(Build.VERSION.SDK_INT >= 21){
                Network[] info = connectivity.getAllNetworks();
                if (info != null) {
                    for (int i = 0; i < info.length; i++) {
                        if (info[i] != null && connectivity.getNetworkInfo(info[i]).isConnected()) {
                            return true;
                        }
                    }
                }
            } else {
                NetworkInfo[] info = connectivity.getAllNetworkInfo();
                if (info != null) {
                    for (int i = 0; i < info.length; i++) {
                        if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                            return true;
                        }
                    }
                }
                final NetworkInfo activeNetwork = connectivity.getActiveNetworkInfo();
                if (activeNetwork != null && activeNetwork.isConnected()) {
                    return true;
                }
            }
        
            return false;
        }
        

        【讨论】:

          【解决方案6】:

          使用following class,更新到最新的可用 Android 版本:Android 10。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-05-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多