Jimmy
Jimmy

Reputation: 10341

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

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() ?

Upvotes: 10

Views: 7317

Answers (2)

ChrLipp
ChrLipp

Reputation: 15668

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.

Upvotes: 5

George
George

Reputation: 1327

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

Upvotes: 5

Related Questions