Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
901 views
in Technique[技术] by (71.8m points)

android - getLastKnownLocation return NULL using GPS_PROVIDER and NETWORK_PROVIDER

This is my GPSTracker constructor class:

    public GPSTracker(Context context) {
    this.mContext = context;
    locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

    Location gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

gpsLocation and networkLocation are always NULL.

getLastKnowLocation method doesn't return me any Location... even if I'm sure that few second before lunching activity (and creating GPSTracker object) the device had network location (I've disabled geotagging after checking that a network location was available)

Tested on Nexus 5 with Android Studio

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yep, getLastKnownLocation() may well return null. The documentation says:

Returns a Location indicating the data from the last known location fix obtained from the given provider.

This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.

If the provider is currently disabled, null is returned.

An Android device does not automatically track its location all the time. The "last known location" is available only if some application has requested the location. So you should not expect to always get a location with getLastKnownLocation(). And even if it returns something the location might not be up-to-date. The Location object has a timestamp which you can read with the getTime() method and an estimated accuracy which you can read with the getAccuracy() method. These can be used to evaluate is the location usable.

If you receive null or a very old location you'll need to request a fresh location. The basic options are just requesting a single location update or continuous updates with a time interval that you can specify.

In either case your class needs to implement the LocationListener interface:

public class GpsTracker implements LocationListener {

    // ...other code goes here...

    @Override
    public void onLocationChanged(Location location) {

        // Do something with 'location' here.   

    }
}

For a single location update you would call [LocationManager.requestSingleUpdate()](http://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate(java.lang.String, android.location.LocationListener, android.os.Looper)) and for continuous updates [LocationManager.requestLocationUpdates()](http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)). In either case there will be a small (or not so small) delay and the LocationListener.onLocationChanged() callback is called when a location is available.

For example you could request a single update from the GPS provider like this:

locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);

For continuous updates from the GPS provider max. every 1 second and only when the location has changed at least 1 meter you could call:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);

And then there are some more variations of the requestSingleUpdate() which you can see in the documentation and on the other hand the Google Play services location APIs but let's not go there. Your question was about getLastKnownLocation() returning null and Stack Overflow has several examples of different ways to request an up-to-date location.

EDIT: This is an old answer. The basic idea is still valid, but check out the Android documentation for modern ways to request location updates.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...