2013-07-11 47 views
2

我試圖檢查移動網絡位置是否位於GPS和/或WiFi &。我目前的代碼只適用於GPS,我試圖嘗試包括網絡提供商,但我收到以下錯誤。檢查是否啓用了GPS和/或移動網絡位置

第一個錯誤

The method isProviderEnabled(String) in the type LocationManager is not applicable for the arguments (String, String)


目前代碼

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER)){ 
      Toast.makeText(this, "GPS is Enabled in your device", Toast.LENGTH_SHORT).show(); 
     }else{ 
      displayAlert(); 
     } 

+1

你沒有使用新的[Fused Location provider](https://developer.android.com/google/play-services/location.html)的原因?它提供的位置更快,更節能,並將所有提供商組合在一起。 – ianhanniballake

回答

3

你必須檢查每個單獨提供:

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || 
    locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ 
     Toast.makeText(this, "GPS/Network is Enabled in your device", 
      Toast.LENGTH_SHORT).show(); 
    }else{ 
     displayAlert(); 
    } 
+0

查看更新錯誤 –

+0

它說你在org.greenbot.technologies.gpsme.MainActivity有一個空指針。 (MainActivity.java:30) - 這條線是什麼? – ianhanniballake

1

如果您看到isProvideEnabled(String)的文檔,則只允許一個字符串作爲參數。所以,你可以做seperately的檢查:

boolean gpsPresent = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
boolean networkProviderPresent = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

然後你可以檢查它們的@ianhanniballake說,或者類似的東西:

if ((!gpsPresent) && (!networkProviderPresent)){ 
    displayAlert(); // Nothing is available to give the location 
}else { 
    if (gpsPresent){ 
     Toast.makeText(this, "GPS is Enabled in your device", Toast.LENGTH_SHORT).show();  
    } 
    if (networkProviderPresent){ 
     Toast.makeText(this, "Network Provider is Present on your device", Toast.LENGTH_SHORT).show(); 
    } 
} 

希望這有助於。

+0

感謝您的代碼,這是有用的 –

+0

這實際上會導致布爾值的nullpointerexception –

+0

如果提供程序爲空,則isProviderEnabled()返回'IllegalArgumentException'。它在哪一行顯示空指針異常? –

相關問題