After GPS is enabled, it will take some time before the GPS is ready. So use this code may get a location that is out of date:
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
I can’t use the location that is out of date. How can I get the location that I need?
Here is a similar question:
Android: getLastKnownLocation out-of-date – how to force location refresh?
From it, I can use LocationListener to get the updated location, which should be not out of date. It’s correct. But how can I handle this case:
I have already enabled GPS, and then start my app. If I stay at one place and don’t move, then onLocationChanged() won’t be called. So I can’t use this method to judge whether the return value of getLastKnownLocation() is not out-of-date?
Any ideas?
You call
requestLocationUpdates()with the minTime of 1000 ms, for everyLocationinonLocationChanged(), you callgetAccuracy(). If the accuracy is acceptable for you, callremoveUpdates().I think the Location object that it returns will contain a time stamp. You can set a tolerance for the time difference between now and the Location’s
getTime()