2014-02-06 242 views
5

我有了這個代碼GPS總是isProviderEnabled返回false

lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
boolean isGPS = lm.isProviderEnabled (LocationManager.GPS_PROVIDER); 

,它總是返回false,即使啓用了GPS。全球定位系統工作正常,但我使用這個布爾值顯示彈出「沒有GPS啓用」。在這種情況下,彈出窗口每次都顯示出來

  1. 我檢查了類似的問題,但它並沒有幫助我。
  2. 是的,我有權在我的清單
  3. 我使用的onResume方法

感謝您的幫助該代碼。

回答

7

嘗試這種方式..

+0

我使用了部分代碼。這'如果'返回真實值 – Lau

+0

乾杯..它爲我工作。 –

+0

他們之所以使用Settings.Secure會比LocationManager.isProviderEnabled返回更好的結果嗎?我問,因爲它似乎在某些設備isProvderEnabled不能按預期工作... – Gyome

0

有時你不得不重新啓動設備,或許GPS的狀態返回失敗那段時間。

2

如果您正在檢查位置是否打開,或者無論它是否使用GPS,那麼您必須注意以下情況,因爲它是我的情況,在設備位置設置中,定位方法已設置爲BatterySaving模式,其中,所述設備僅使用無線網絡的移動網絡來估計位置:

enter image description here

因此,GPS甚至沒有在更新位置使用,所以位置圖標不會出現在狀態除了位置提供商之外,酒吧將被列爲網絡不是GPS

所以,要解決這個問題,你必須檢查提供商是否包含gps或包含network

private boolean checkIfLocationOpened() { 
    String provider = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
    if (provider.contains("gps") || provider.contains("network")) 
     return true; 
    } 
    // otherwise return false 
    return false; 
} 

,如果你想使用LocationManager做到這一點:

private boolean checkIfLocationOpened() { 
    final LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
    if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) || manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ 
     return true; 
    } 
    // otherwise return false 
    return false; 
} 
0

要解決GPS isProviderEnabled在Android 6上總是返回false,將其添加到您的清單中:

<uses-feature android:name="android.hardware.location.gps" /> 

鈦在上面的android 5.0 &上找不到GPS硬件,因此它一直返回false。添加上面的行來解決問題。