2011-04-29 152 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; 
}