Android getLastKnownLocation doesn’t update Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Android getLastKnownLocation doesn’t update without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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?

Answer

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

We are here to answer your question about Android getLastKnownLocation doesn’t update - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji