2011-04-29 112 views
1

我正在检查Android手机上的网络连接。即使当我在“设置 - >位置 - >使用无线网络 - >关闭”和“启用GPS状态灯 - >关闭”中关闭网络连接并禁用WiFi时,它始终显示“Internet连接存在”。网络连接总是在手机上检测到,即使没有网络

我使用下面的代码:

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" /> 

我在做什么错?

+0

你使用真实手机或模拟器? – 2011-04-29 05:36:31

+0

我使用真正的手机 – 2011-04-29 05:38:00

回答

1

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

为了彻底切断接入互联网,尝试飞行模式(设置 - >无线&网络设置 - >飞行模式)

0
/** 
    * 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()); 
       } 
      } 

     } 
+0

gps有另一个代码...因为这个代码只检查网络提供商的可用性。 – Hanry 2011-04-29 05:42:24

+0

是的,这是为网络供应商提供服务 – Dharmendra 2011-04-29 05:45:21

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