Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: getLastKnownLocation out-of-date - how to force location refresh?

I'm using

myLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

to retrieve the current location at the start-up of my application.

As the Android documentation states, this location can be "out-of-date", since the method returns the location when the GPS was used the last time.

How can I actively request the current location from the GPS? I thought about using LocationListener, however that might be a bit of an overkill, since I only need the location once (at the start of my app).

Any ideas?

like image 450
znq Avatar asked Jan 13 '10 11:01

znq


1 Answers

Your initial intuition is correct - you need to use a LocationListener to request updates. Given that you require only a single position, you can unregister the LocationListener after the first value returns.

In practice though, it's probably wise to listen for a little bit longer. Location Based Services (particularly GPS) have a tendency to 'jump around' a bit when they first get their fix. Your best bet is to listen for a set amount of time, or a set number of updates, or until a certain level of accuracy has been achieved (the Location Listener will return the accuracy of the position returned).

like image 179
Reto Meier Avatar answered Sep 23 '22 19:09

Reto Meier