Search code examples
androidgeolocationandroid-4.2-jelly-bean

Android 4.2 Location service issues


When I run my app on Android 4.2 OS phone, I can't seem to be able to get any values returned from getLAstKnownLocation ( always null ) The weird thing is that the Mapview works fine and it shows me my current location !!

Any ideas how to solve this issue ?

here is my code

in onCreate:

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

then the listener:

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

Solution

  • getLastKnownLocation will return a null when the provider is disabled. Or when there is no LastKnownLocation.

    On the line: provider = mlocManager.getBestProvider(criteria, false);

    your provider is asking for the BestProvider including disabled providers. Try using true at the end of the statement and see if that works.

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