3

当我在 Android 4.2 OS 手机上运行我的应用程序时,我似乎无法获得从 getLAstKnownLocation 返回的任何值(始终为 null) 奇怪的是 Mapview 工作正常,它显示了我当前的位置!

任何想法如何解决这个问题?

这是我的代码

在 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");
    }
}
4

1 回答 1

0

当提供程序被禁用时,getLastKnownLocation 将返回 null。或者当没有 LastKnownLocation 时。

在线上:provider = mlocManager.getBestProvider(criteria, false);

您的提供者要求 BestProvider 包括禁用的提供者。尝试在语句末尾使用 true ,看看是否有效。

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

于 2012-12-16T02:08:12.300 回答