【问题标题】:network connection always detected on handset, even with no network即使没有网络,也始终在手机上检测到网络连接
【发布时间】:2011-08-15 06:09:09
【问题描述】:

我正在真正的手机上检查 Android 中的网络连接。它总是显示“Internet Connection Present”,即使我在“设置 -> 位置 -> 使用无线网络 -> 关闭”和“启用 GPS statelites -> 关闭”中关闭网络连接并禁用 wifi。

我正在使用以下代码:

  private boolean checkInternetConnection() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        // test for connection
        if (cm.getActiveNetworkInfo() != null
                && cm.getActiveNetworkInfo().isAvailable()
                && cm.getActiveNetworkInfo().isConnected()) {
            Log.e("TAG", "Internet Connection Present");
            return true;
        } else {
            Log.e("TAG", "Internet Connection Not Present");
            return false;
        }

    }

我已设置权限:

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

我做错了什么?

【问题讨论】:

  • 你用的是真机还是模拟器?

标签: android networking


【解决方案1】:

“设置->位置->使用无线网络(关闭)和使用 GPS 卫星(关闭)”是用于确定位置的设置,而不是禁用对互联网的访问。

为了完全切断对互联网的访问,请尝试飞行模式(设置 -> 无线和网络设置 -> 飞行模式)

【讨论】:

    【解决方案2】:
    public static boolean isNetworkAvailable(Context context)
    {
        ConnectivityManager connectivity = (ConnectivityManager)context.getSystemServic(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) 
        {
            Log.w("tag", "couldn't get connectivity manager");
        }
        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;
                    }
                }
            }
        }
            return false;
    }
    

    【讨论】:

      【解决方案3】:
      /**
           * THIS IS FUNCTION FOR CHECKING INTERNET CONNECTION
           * @return TRUE IF INTERNET IS PRESENT ELSE RETURN FALSE
           */
          public boolean checkInternetConnection() {
              ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      
              if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {
                  return true;
      
              } else {
                  return false;
              }
          }
      

      这在我的项目中有效,并且也在设备上进行了测试

      如果您想检查 GPS 提供商的可用性,请尝试此代码

      LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
              String provider = null;
      
              if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                  provider = LocationManager.NETWORK_PROVIDER;
      
              } else if ( lm.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
                  provider = LocationManager.GPS_PROVIDER ;
      
              } else {
                  Toast.makeText(this, "Provider Not available", 3000).show();
              }
      
              if(provider != null) {
      
      
                  lm.requestLocationUpdates(provider, 30000, 100, this);
                  Location location = lm.getLastKnownLocation(provider);
      
                  if(location != null) {
                      currentGeopoint = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
      
                      if(currentGeopoint != null) {
                          currentaddress = getAddressFromLatitudeAndLongitude(location.getLatitude(), location.getLongitude());
                      }
                  }
      
              }
      

      【讨论】:

      • 对于 gps 有另一个代码...因为此代码仅检查网络提供商的可用性。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      相关资源
      最近更新 更多