2012-11-25 46 views
3

當我在Android 4.2操作系統手機上運行我的應用程序時,我似乎無法獲得從getLAstKnownLocation返回的任何值(始終爲空) 奇怪的是,Mapview工作正常,它顯示我當前的位置!Android 4.2位置服務問題

任何想法如何解決這個問題?

這裏是我的代碼

中的onCreate

mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
Criteria criteria = new Criteria(); 
provider = mlocManager.getBestProvider(criteria, false); 
mlocManager.requestLocationUpdates(provider, 0, 0, geoHandler); 

那麼聽衆:

public class GeoUpdateHandler implements LocationListener { 
    public void onLocationChanged(Location location) { 
     // called when the listener is notified with a location update from the GPS 
     int lat = (int) (location.getLatitude() * 1E6); 
     int lng = (int) (location.getLongitude() * 1E6); 
     new GeoPoint(lat, lng); 

     Log.d(this.getClass().getSimpleName(), "onLocationChanged " + lat); 
     //   mlocManager.removeUpdates(this); 
    } 

    public void onProviderDisabled(String provider) { 
     // called when the GPS provider is turned off (user turning off the GPS on the phone) 
     Log.d(TAG, "DISABLED"); 
    } 

    public void onProviderEnabled(String provider) { 
     // called when the GPS provider is turned on (user turning on the GPS on the phone) 
     Log.d(TAG, "ENABLED"); 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // called when the status of the GPS provider changes 
     Log.d(TAG, "CHANGED"); 
    } 
} 

回答

0

getLastKnownLocation將返回一個空當供應商將被禁用。或者當沒有LastKnownLocation時。

就行了:provider = mlocManager.getBestProvider(criteria, false);

您的供應商是要求在BestProvider包括殘疾提供商。嘗試在聲明的最後使用true,看看是否有效。

http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)

+0

感謝您的回答。使得它 「真」 +添加下列行它上面使它工作: \t \t \t \t \t \t criteria.setAccuracy(Criteria.ACCURACY_FINE); – AlAsiri