Android 4.2位置服务问题

时间:2012-11-25 19:45:56

标签: android geolocation android-4.2-jelly-bean

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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