There are a number of workarounds for detecting the possibility of a fake location being returned by the LocationManager API. those include checking if the mock location setting is enabled and checking the list of installed apps for the ones using the mock location permission. if any of the above happens we could go with removing the TestProvider from the current LocationManager instance and getting a location again, just in case the api returns bogus results. however the above approach wont work with the FusedLocationAPI. is it always granted to return the real location ? or should i consider changing my code to use the LocationManager ?
because of the nature of product i'm working on, it is necessary to be sure of the location.
thanks in advance
Related
I've read some questions about this, but I didn't quite find an answer that I needed. I want to understand how getLastLocation works internally. How last known location cached value is updated?
We have requirement where we need to get last known location of the device. Can anyone explain how getLastLocation works? I have seen that getLastLocation API returns null even if location-setting is ON. At the same time if I checked Google Map app and it is able to get the location (of-course it must be using requestLocationUpdates or any other API).
On my Nexus-5, I observed that getLastLocation returns null even after I launch Google Map app and then launch my app again. Even if location is detected correctly in Google Map app, getLastLocation API returns null.
How Android system updates the last known location? Does it update only when some app requests for location via requestLocationUpdates API?
If Android system updates the last known location automatically then why do getLastLocation API returns null?
getLastLocation API
Getting last known location
Points to be noted:
Location permission is granted.
Location Setting is ON
Same code works on few devices, but fails on few devices.
I am trying to get location from Google's FusedLocationProviderAPI and Android Framework LocationManager API.
I have checked all existing questions on Stackoverflow
It would be great if anyone can explain under what circumstances getLastLocation API can return null even if location setting is ON. Does anyone know how this works internally?
getLastLocation : This method will give you the last location available on your device.
In some cases if your device don't have any last location then it will return null.
You should use onLocationChanged method for current location.
Please see example here you will get it
https://developer.android.com/training/location/receive-location-updates.html
According to doc
public abstract Location getLastLocation (GoogleApiClient client)
Returns the best most recent location currently available.
If a location is not available, which should happen very rarely, null
will be returned. The best accuracy available while respecting the
location permissions will be returned.
This method provides a simplified way to get location. It is
particularly well suited for applications that do not require an
accurate location and that do not want to maintain extra logic for
location updates.
Hope these help you.
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 writing an app which needs to get any fresh location. Accuracy does not metter. My app will mainly be used in buildings, so I am using Network location provider as a main location source.
I have troubles on some devices as follows:
My code can't access any location. But when I start Google Maps on the same device - it shows correct location. I switch back to my program and it does not see any location again.
My code does roughly the following:
Check if Network location provider is available with code below:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
If network provider is enabled - requirest single location update:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Code also starts Timer for 5 seconds. If code does not get requested location update during 5 seconds, then timer is iterating throuhg all available providers trying to get last known location and pick up the best one:
Location location = locationManager.getLastKnownLocation(provider);
The result of all these moves is that network provider is On, but code does not get any updates. Timer starts, loops through all providers and all of them does not have any last known location. getLastKnownLocation method returns null for all of them. The most strage thing is that Google Maps got correct location just a few seconds before this. I've rebooted device with no effect.
I examined best practices of getting location here. They seem to use the same approch as I currently do. Are there any tricks I am missing?
This problem occur in some devices the solution is try to get location from GPS if fails then get location from WIFI device
I use network provider to get location in Android application. Sometimes I found the location would not be changed even after I arrived at a new place. At the same time, I found the locations were changed in Google Map. So I quit and restarted my application, and the locations wasn't changed either. But after a long period, or I reboot the phone, I could see the new location.
Does any one know what's the reason ? I have used getLastKnownLocation() to get the latest location in my application.
Update:
When this problem happened, I found the cellid and LAC was changed while the location wasn't changed. So it's very strange. I think Google Map may use cellid & LAC to get the location directly, so it can get the correct location.
More update:
When this problem happened, I used the cellid and LAC to get the location via HTTP POST request from Google server. This location was correct.
BTW, I found my question was like this question: Android network location takes hours to update location
I wouldn't trust getLastKnownLocation(). In my experience this isn't reliable.
You'd be better off requesting location callbacks using LocationManager.requestLocationUpdates(). If you only need the location once you can then unregister for the callbacks using LocationManager.removeUpdates() after that. If your application needs to be notified when the location changes then you can register for callbacks with a reasonable minium-time and/or minimum-distance parameter (based on whatever your application requires.
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