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

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

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

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.

Related

Getting last known location from Google Play Services while device location is off

I'm using FusedLocationApi to get device's last known location from Google Play Services.
My understanding is that getLastLocation delivers the last known location (assuming the needed permissions are granted) regardless of which app has requested the location even when the device location is off but does not work as such.
When the location is on it works as expected; But when it's off it returns null though it has previously retrieved the location for example from the Google Maps app.
Should it work like this or something's amiss here?
When you turn off a location provider on the device (GPS, Network, etc), it clears the last known location. Any apps that still shows a point of some kind when location is turned off probably cached it.
So the answer is that you should receive null for getLastLocation if location services is turned off.

FusedLocationProviderAPI not receiving location if GPS is off

I need to run a service continuously to update my location data to server, I am using FusedLocationProviderAPI to retrieve location and priority as PRIORITY_BALANCED_POWER_ACCURACY, but when GPS is switched off no location is received.
I thought fused provider manages this based on the priority given, but that is not the case, can anyone tell me what can be done to solve this.
Thanks.
Turning off the location on your device turns off all the location services. If you go to Settings -> Location -> Mode you'll see the location options. Switch it to "Battery saving (Networks only)" if you don't want to use the GPS.

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

Android GPS location isn't as accurate as network location

I've got an app which sets up two location listeners (one for GPS and one for network) and then chooses the location from the best one available, which should be GPS. However, if GPS is turned on, it will always choose the GPS location, even if the person starts the app up indoors. This has led to the GPS location being used even if the last known GPS location is from miles away and the network location is actually much more accurate.
Is there any way around this issue or is it just something that will need to be accepted as an issue with using GPS & network? Is the standard practice just to assume that GPS is always more accurate even though it's possible that it might not be in certain instances?
If you are using the network provider to fetch the location they are not very accurate when compared to GPS. So it can be a good idea to fetch the accuracy of your position using network provider when you are indoors as GPS doesnt function well indoors and you can use GPS to get the location updates when you are outdoor.
You can use GetAccuracy() method of Location class and see for the value it returns. Let Say if getAccuracy() retruns 25 then you can leave this value wait for another gps value until you get the desired value. Remember the value it returns is the radius of the circle in meters.

LocationService not retrieving location using only Network

I have re-read the whole documentation and as I understand, this API intelligently fuses the providers to give you an accurate result.
The Fused Location Provider intelligently manages the underlying location technology and gives you the best location according to your needs.
To my understanding, it will use Network and WiFi and GPS to give you a location. Now it is only logical to assume if one of those 3 is unavailable, it will use the remaining 2.
My problem is I'm testing my app to get Location when both WiFi and GPS are off. That will leave Location Service with the network provider only. But I'm getting Latitude and Longitude = 0.0
Long story short, either way I'm wrong in assuming that it will work relying only on network (no GPS, no WiFi) or there is something else I don't know or haven't read yet. Which of this 2 is my issue?
PS: my LocationRequest configuration is set to setInterval(30000) to setFastestInterval(60000) (30 and 60 seconds respectively, for each update.

Categories

Resources