2011-08-25 118 views
2

我測試的設備上我的Android應用程序,當我使用的LocationManager在Android的位置返回null

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

但是,如果我用NETWORK_PROVIDER代替GPS_PROVIDER它總是失敗,它的工作原理。 currentLocation返回null。

我該如何處理?有什麼具體的我需要把我的locationListener?

謝謝!

回答

3

GPS_PROVIDER可能無法在有限的地方工作,假設在房子內或類似的東西。你應該這樣編程,如果GPS提供商給出空位置,那麼你使用網絡提供商作爲替代品,儘管它的準確度不是很高。 這裏還有另一個問題。你的代碼應該如下:

 if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) 
{ 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
    currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
} 
+1

它啓用,然後也給null –

1

首先,檢查設備狀態欄上的GPS圖標是否已停止閃爍,表明您確實有GPS鎖定。

如果位置當前不知道,例如,如果您缺少GPS鎖,則該調用返回null。

+0

有沒有一種方法來以編程方式保護GPS鎖? – Magnus

+0

GPS鎖意味着設備已從GPS衛星成功獲取位置。所以,當你詢問GPS的位置時,設備開始連接到衛星,這需要一定的時間,如果你在室內,可能會不成功。 我建議您在開始開發之前先使用手機上已有的支持GPS的應用程序,以便了解GPS如何工作。 – Zds