Home » Android » getLastKnownLocation() Android, when the Last Known Location is updated?

getLastKnownLocation() Android, when the Last Known Location is updated?

Posted by: admin June 15, 2020 Leave a comment

Questions:

getBestKnownLocation 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.

when the Last Known Location updated in Android? is it updated if when there’s an application listening for the location provider, if so what if there’s no application that it ask for the location and then you asked for LastKnownLocation() ?

How to&Answers:

I thinki LastKnownLocation() is updating when some programs in your phone use this function requestLocationUpdates.

Answer:

Please see Start location strategy

I did some investigations: I turned on GPS and waited to get a fix. Then I turned GPS off and drove 50km (31 miles). Then I used the code from A Deep Dive Into Location to get all the getLastKnownLocation. I tried it twice, first with GPS turned off and second with GPS turned on, but without a fix:

1) with GPS turned off I got
– Provider: network, correct location with accuracy 680m
– Provider: passive (mProvider=network), same location as above, same time as above
– Provider: gps, location null

So I learned that when gps is turned off you get no getLastKnownLocation from the GPS location provider.

2) with GPS turned on I got
– Provider: network, correct location with accuracy 652m
– Provider: passive (mProvider=network), same location as above, same time as above
– Provider: gps, location as it was 2h earlier with accuracy 12m, time was also 2h earlier

Here I learned that old messages are not invalidated, even it is obvious that they are wrong.

So to sum it up: when a provider is active, it stores the last received location retreivable via getLastKnownLocation. If the provider is deactivated, you don’t even get getLastKnownLocation. Please note that I tested this with the GPS-Provider, other providers may react in a different way.