1. 程式人生 > >關於定位時,getLastKnownLocation返回值為null所碰到的問題

關於定位時,getLastKnownLocation返回值為null所碰到的問題

在寫到gps定位的時候,getLastKnownLocation返回的location一直都是null,註冊的LocationListener裡面的回撥方法也沒有任何反映。僅僅當我在手機設定關閉或者開啟gps定位的時候,onProviderEnabled 和 onProviderDisabled 才會相繼呼叫。

我的location獲取是這樣寫的

	Location location = mLocationManager.getLastKnownLocation(getProvider());
		setLocationText(location);
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, locationListener);
 其中location的返回一直都是null,查閱了一些資料,也沒有得到正確的解說,有人說用while做一個location==null 就一直請求(
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, locationListener);
),總感覺這樣不妥,後面我修改了
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, locationListener);
為mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 1, locationListener);
location這才有值

接下來我探究了一下network_provider,當我僅開啟wifi連線location返回值有值,當我僅開啟gps時location也有返回值。這樣看來那個network引數,是不影響從哪個途徑裡面拿location的值。但是如果設定為gps-provider返回值就為空。

補充:當我僅開啟wifi連線getprovoder()返回為network,當我僅開啟getprovoder()返回值為gps。

為自己所發現mark一下。o(∩_∩)o 哈哈--