Don Cheadle
Don Cheadle

Reputation: 5576

Does Location Services' `getLastKnownLocation()` check phone's current GPS location?

Some resources say getLastKnownLocation() merely gives a location of some previous app's Location Change Listener.

But one thing I feel is missing from the conversation -- if the phone has GPS enabled, isn't this GPS tracking/updating as the phone moves? So if I call getLastKnownLocation(), isn't it getting the current GPS from the phone's constantly-updated GPS?

If so, then why do people warn against using it / accuse it of potentially getting a "stale" location? If the GPS is being tracked / updated, and getLastKnownLocation() makes a one-time grab of it's current position, what makes getLastKnownLocation() bad?

What am I mistaken about the Locations service or GPS?

Upvotes: 1

Views: 268

Answers (2)

Illya Gerasymchuk
Illya Gerasymchuk

Reputation: 462

The documentation says: Returns a Location indicating the data from the last known location fix obtained from the given provider.

Also, about the "out of date location": 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. ("This" refers to getting the last known location).

Also, taken from LocationProvider docs: Each provider has a set of criteria under which it may be used; for example, some providers require GPS hardware and visibility to a number of satellites; others require the use of the cellular radio, or access to a specific carrier's network, or to the internet. They may also have different battery consumption characteristics or monetary costs to the user.

if the phone has GPS enabled, isn't this GPS tracking/updating as the phone moves?

Well, it is, but not constantly. This has to do with battery consumption, if the phone was constantly updating the GPS location, the battery life would suffer a lot. The GPS location is usually updated when some app requests it. But even that request is not guaranteed to succeed. For example, suppose your going into a tunnel. Some application requests to update the GPS coordinates right before entering the tunnel and it succeeds. Now, you've entered the tunnel, which is 5km long. Most probably your device won't be able to get a GPS fix from the tunnel, so for the next 5km (at least) getLastKnownLocation() will return an outdated value, since the devices last known location was at the entrance of the tunnel.

What you could do is explicitly request to update the GPS location, that however might take some time and there are no guarantees that it will succeed.

Upvotes: 2

Victory
Victory

Reputation: 5890

You have the assumption "if the phone has GPS enabled, isn't this GPS tracking/updating as the phone moves". This assumption is incorrect. The GPS functionality takes up a lot of battery life so it should be used sparingly and almost certainly not all the time.

Upvotes: 1

Related Questions