Does it give the user's current location if the device is online? How does it determine the last known location?
LocationManager docs
Taken from the docs & reworded
getLastKnownLocation returns the last known location fix obtained from the given provider.
The device does not need to start a provider to get this information so good for battery life.
However, if the users phone has been sleeping or turned off and has moved the location will be out-of-date.
If you need the location to be up-to-date I wouldn't use this method, instead request a single update, depending on your applications requirements.
Note: requesting location updates from an available provider will effect the users battery life if used irresponsibly
Related
I've been working on an app that needs to be location-aware, and I've noticed that there are two (or more) methods of receiving location: with Google Play services (as seen here developer.android.com/training/location/retrieve-current.html#GetLocation) and with Location Manager, Providers etc. (as seen here http://www.vogella.com/tutorials/AndroidLocationAPI/article.html#locationapi_criteria).
What is the difference between these methods (if there is any)? Which one is more accurate?
edit: ok, I see that I sent the wrong link on the first thing. Won't this code (http://developer.android.com/training/location/receive-location-updates.html) give me location updates? Generally, what's the most accurate way to get my location?
The one with the GPS is accurate and that which is based on Network is not. Google Play Service use FUSE api to get the GPS location first, if the location is found (that's great), otherwise it will try to get location fix from Network Tower. In Short the one with GPS is accurate
The first method provides the details of LastKnownLocation. ie. the last location received from GPS or network provider when you or other apps accessed the location services. After that there are chances you moved a lot and it need not be your current location. So if You are planning to create an application that requires accurate location tracing, You should fetch the location as in the "Vogella" method. If the current location is unavailable, you can try using the last known location (As a plan B :-)).
I am developing an app which can record user's location througout the day so app can find out the approx lifestyle of user. app has to collect data in the background at some interval so i need a best strategy to invoke a location update request to LocationManager of android. But it must be optimized in terms of battery and balanced in terms of accuracy.
Right now the flow in my mind is,
iterate through the location providers, give higher priority to NETWORK (as it consumes less power), o this for each,
get the last known location
get the current loction
chek if current is better than last
if yes make this the best estimate, otherwise get location from other provider
i know two parameters which can decide when to invoke location update request,
minimum interval
minimum distance
first i would give them some default value but these must be updated later by using the location history of user and considering other factors in mind like
reduce the frequency of update request if battery is < 75% and further reduce it when
battery is < 50%
increase frequency of update request when user is moving fast (Activity Recognition Class
from google location services can help here)
if we predict the time windows in which user does not move (from history), decrease the
frquency of location updates
use GPS minimal time because it consumes more battery
So these are the pieces that came to my mind but it looks like a mess right now because it is not well structured. So if somebody can help me to add something more in this or can suggest some better strategy (battery must be optimized) i would be very thankful and it was long so please control your emotions if you think i have wasted your time. Thanks
In order to get a good accuracy, Google provides an algorithm that provides, in my opinion, good results. You can find it there : reference
GPS take so time to have a fix, I would say about a minutes (depends on the device).
I would suggest you to look at the Passive Mode, which allows you to receive position without making a fix by using other applications requests.
passive: A special location provider for receiving locations without actually initiating a location fix. This provider can be used to passively receive location updates when other applications or services request them without actually requesting the locations yourself. This provider will return locations generated by other providers. Requires the permission android.permission.ACCESS_FINE_LOCATION, although if the GPS is not enabled this provider might only return coarse fixes.
gps tutorial
Hope it helps !
I've read the documentation and this.
What I don't understand is why this function isBetterLocation() even exists. The function tells the difference between "current location fix" and "new location". But what are those 2 locations really?
I can think of
Last location stored in application preferences, database etc. and location retrieved from location provider e.g. GPS
Location retrieved from location provider and latest location gotten through onLocationChanged()
In either case, the latter is more up-to-date; therefore, more accurate. So, why bother making comparison at all?
The only explanation I can think of is that more up-to-date location information isn't always more accurate. Is that true?
Take a look here http://developer.android.com/guide/topics/location/strategies.html, there is an explanation on this page (android documentation)
I am building an app that can use a user's current location on certain actions. Location is more of a benefit to the user rather than a critical part of the process. I'm only interested in very rough accuracy and it can be off by 5 or even 10 miles and still be of value. General plan was to see if the network provider was enabled and then just do
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
Assuming that no other apps are running, will the phone automatically periodically update the network location? I.e. there will likely always be a value returned by this code which will meet my needs?
EDIT:
I've been doing a bit more research and I think the question I was really trying to ask here was if the Android operating system or the phone itself would update the last known location for the network provider as the phone locked onto different cell phone towers or wifi networks. The answer appears to be no. After force stopping all apps on my phone which I know to interaction with location services, the last known location has stopped updating. So, I'm guessing that these days chances are that most phones will have some location services running in the background updating the last known location but the phone itself won't do it. Hence I think I'll be going with some form of requesting location updates if the last known location is too old.
getLastKnownLocation() only returns the last fix. So if no location providers are being updated the return value of getLastKnownLocation() will not change. The location object will also provide you with accuracy and time of the fix.
I would look at this post for more information. You could use some version of the one shot location.
http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html
Frohnzie is correct about getLastKnownLocation. Getting location is an expensive operation, so it only happens when an app requests it. I've often driven 30 miles or so, opened the foursquare app on my phone, and it showed me venus that were 30 miles away. Clearly they were using getLastKnownLocation.
For your use case, I would recommend LocationManager.requestLocationUpdates. You can specify an accuracy and a timeout. So if the phone can't quickly find a rough estimate of the user's location, you can just forego location since it is not crucial to your application.
I want to make sure the location received by the LocationListener is a "real" one and doesn't come from a spoofed source (I.e the Application Location Spoofer). I don't care about "but the user sometimes want to spoof the location" - it's about an app which doesnt get distributed over the android market. The application does not need the location to be that accurate - it just has to be "real".
Do you think there is a way to check if the location has been spoofed?What do you think is a good way to prevent location spoofing?Is there maybe a android-method which gives me the real location nevertheless?Is there maybe a android-method to determine if the location has been spoofed?
Thoughts I had:
Check on a blacklist if a location-spoofing app is installed
Enable/disable different location providers and check against their returned locations
Run a background service watching the location (in a passive way) and check for sudden location changes
Please give me your thoughts and input to this issue.
From the Provider name: 'gps' or 'network' are proper
Most spoofers forget to send GPS status updates. That is something that you can use to your advantage
// returns true if mock location enabled, false if not enabled.
if (Settings.Secure.getString(getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION).equals("0"))
return false;
else return true;
The best method is this :
Mock location providers do not send NMEA data. So create a NMEA listener. If you don't get NMEA update but you get valid GPS updates in onLocationChanged() a spoof is being used.
Have a look on "Maintaining a current best estimate" topic on the following page:
Maintaining a current best estimate