Bcct
Bcct

Reputation: 27

Android getLastKnownLocation doesn't update

I'm making an app that gets the user location, for that I'm using getLastKnownLocation, the problem is, if I change the location of my virtual phone to somewhere else, getLastKnownLocation, still gives the same location, it only updates if I open Google Maps. Is there any way I can get the actual location of the device?

Upvotes: 1

Views: 476

Answers (1)

Robert
Robert

Reputation: 42799

The behavior of LocationManager.getLastKnownLocation you describe is exactly the way this method has been defined:

Gets the last known location from the given provider, or null if there is no last known location. The returned location may be quite old in some circumstances, so the age of the location should always be checked. This will never activate sensors to compute a new location, and will only ever return a cached location.

Hence if any other app is used that requests the actual position (such as Google Maps) the cached value is refreshed and you can retrieve it via getLastKnownLocation.

If you actively want to retrieve a new location you should use getCurrentLocation or continue to use getLastKnownLocation but force a location update prior to calling it via requestSingleUpdate

Upvotes: 1

Related Questions