I am trying to get user location with low battery consumption and without GPS (location). Milimetric coordinates are not necessary for me. So I decided to use
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
and also at this google developer docs https://developer.android.com/guide/topics/location/strategies.html it says:
Permission for ACCESS_COARSE_LOCATION allows access only to
NETWORK_PROVIDER.
I dont actually understand why this API doesn't give user current coordinates without location being enabled. In my opinion, if I use COARSE_LOCATİON instead of FINE_LOCATION, API should not use location.
I am also trying "Basic Location Sample" at https://github.com/googlesamples/android-play-location/tree/master/BasicLocationSample
You are requesting for user's LOCATION it may be with FINE LOCATION or COURSE LOCATION. User needs to AUTHORIZE the app for doing that.
The only difference is that the device will spend less energy trying to get the user's location and won't get exactly location.
Related
I'm new to android but my current project requires to work with gps tracking. I've searched the web to find some answers, and some topics confused me. Can you tell me please, I'm not sure I've understand:
Where do we need to use coarse and when fine?
whats the main difference between using NETWORK_PROVIDER and GPS_PROVIDER?
From the documentation:
If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. Permission for ACCESS_COARSE_LOCATION allows access only to NETWORK_PROVIDER.
So in short, the difference is the level of details of the location you get.
The network provider determines the location of the users using cell towers, wifi access points etc. Distance between towers and user’s position are considered in the case of cell towers. This location provider offers a faster response but can be fuzzy.
The GPS provider determines the location of the users using satellites. This is usually more precise, but takes more time.
Note: there is also a PASSIVE_PROVIDER, which doesn't actively requests location, but is able to eavesdrop on other app's requests, i.e. it gets location information when other apps ask for it.
Fused Location Provider gives the location technology and gives you the best location.This api gives the user access to the best location without using much power on the device.
Add to your manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
In activity code
private FusedLocationProviderClient flpClient;
flpClient = LocationServices.getFusedLocationProviderClient(this);
to get the last known location
flpClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null) {
// Logic to handle location object
}
}
});
Look at the api for further explanation https://developer.android.com/training/location/
can two different api calls each requiring a different permission produce the same results?
e.g. to get device coarse location, from Android documentation, it is an approximate location derived from network location.. can an app retrieve network location using another api call that does not require ACCESS_COARSE_LOCATION permission?
In other words, according to Android documentation, regarding LocationManager, "A location provider provides periodic reports on the geographical location of the device....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" and all requires either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions.
if an app doesn't ask for these permissions yet it asks for ACCESS_NETWORK_STATE, ACCESS_WIFI_STATE, and INTERNET can the app infer location? if so, what API calls are responsible for this to happen, I read the API guide, connectivity part, and searched the classes under android.net.wifi but could not find what would create such situation.
is there any work or a resource that groups Android api calls by required permissions?? e.g. a list of all api calls under ACCESS_COARSE_LOCATION permission, a list of all api calls under ACCESS_FINE_LOCATION permission and so on!
Hope I clearly asked my questions and thanks in advance!
According to the documentation:
Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. (Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.)
You should take in consideration that coarse location (battery friendly) is not so precise as fine location (not battery friendly), AND usually they are required both because a user can decide to enable from device network location and disable GPS, it's your duty to optimize your app so it will not kill the battery. Information from above (and more about location) can be found here http://developer.android.com/guide/topics/location/strategies.html
the previous link has some explanations about the location, with some examples.
And for future reference, you might find this table useful http://developer.android.com/guide/topics/manifest/uses-feature-element.html#features-reference
My app only needs very rough location data, so I originally set my manifest permissions to ACCESS_COARSE_LOCATION, and set up a location listener for NETWORK_PROVIDER. This gave me exactly the kind of rough location estimate I needed, but only with Google Location Services turned on.
I was expecting that if the user only had GPS enabled, that I would still receive a rough estimate of their location. But it seems like the only way to get ANY location information from the GPS_PROVIDER is by using the ACCESS_FINE_LOCATION permission.
So, is it true that with only GPS enabled, an app cannot receive location information unless it has the ACCESS_FINE_LOCATION permission? In other words, the GPS_PROVIDER can't send rough location estimates if the app only has ACCESS_COARSE_LOCATION permission?
So, is it true that with only GPS enabled, an app cannot receive location information unless it has the ACCESS_FINE_LOCATION permission?
Generally speaking, yes.
Quoting the documentation for LocationManager:
If your application only has the coarse permission then it will not have access to the GPS or passive location providers. Other providers will still return location results, but the update rate will be throttled and the exact location will be obfuscated to a coarse level of accuracy.
In other words, the GPS_PROVIDER can't send rough location estimates if the app only has ACCESS_COARSE_LOCATION permission?
Quoting the Android 4.2 release notes:
Compared to previous versions of Android, user location results may be less accurate if your app requests the ACCESS_COARSE_LOCATION permission but does not request the ACCESS_FINE_LOCATION permission.
To meet the privacy expectations of users when your app requests permission for coarse location (and not fine location), the system will not provide a user location estimate that’s more accurate than a city block.
However, I am assuming that this does not supersede the "will not have access to the GPS" statement from LocationManager. I assume that this means that NETWORK_PROVIDER data might be inhibited, if it is deemed too accurate.
I would like to get accurate location from NETWORK_PROVIDER keeping GPS turned OFF in order to save battery. This seems impossible becasue android, when ACCESS_FINE_LOCATION is specified, turns on GPS even when location is not requested to GPS_PROVIDER.
I have added:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
into the Manifest. I needed that in order to avoid android obfuscating accurate position.
I registered updates just using NETWORK_PROVIDER and not GPS_PROVIDER:
myLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
60000, 0, locationListener);
GPS turns on anyhow. How can I avoid it (without forcing the user to disable GPS, that actually works but is undesired)?
Sounds strange, if you haven't requested location updates from the gps-provider, it shouldn't boot up the GPS.
Let me give you a piece of advice though - don't count on the network provider to give you 'fine' accuracy unless you're sitting on a wifi that google knows the position of.
Positioning based on cell towers usually have accuracy of about 500 - 2000m, perhaps 3-500m in city centers.
Use either ACCESS_FINE_LOCATION is you want GPS to be used or ACCESS_COARSE_LOCATION if you don't want to use GPS.
If your positioning technologies are disabled to save power, how can you expect to get an accurate position? That would be like expecting a car to take you to work but not wanting to put gas in it. tanstaafl.
As mentioned above, WiFi positioning can give you pretty good location acuracy if you are around a couple of APNs that Google knows about. Given enough time and enough people using their phones, Google will eventually learn every APN that doesn't move.
In my application I would like to determine the user's current location. I do however have a couple of questions in this regard:
There are different Location Providers, which one is the most accurate? The GPS Provider or the Network Provider?
In how far do those available provider differ? How do they function?
Could you please provide me with some code-snippets or tutorials on how to get started with implementing GPS functionality in my application?
There are 3 location providers in Android.
They are:
gps –> (GPS, AGPS): Name of the GPS location provider. This
provider determines location using satellites. Depending on
conditions, this provider may take a while to return a location fix.
Requires the permission android.permission.ACCESS_FINE_LOCATION.
network –> (AGPS, CellID, WiFi MACID): Name of the network
location provider. This provider determines location based on
availability of cell tower and WiFi access points. Results are
retrieved by means of a network lookup. Requires either of the
permissions android.permission.ACCESS_COARSE_LOCATION or
android.permission.ACCESS_FINE_LOCATION.
passive –> (CellID, WiFi MACID): 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. This is what
Android calls these location providers, however, the underlying
technologies to make this stuff work is mapped to the specific set of
hardware and telco provided capabilities (network service).
The best way is to use the “network” or “passive” provider first,
and then fallback on “gps”, and depending on the task, switch between
providers. This covers all cases, and provides a lowest common
denominator service (in the worst case) and great service (in the best
case).
Article Reference : Android Location Providers - gps, network, passive By Nazmul Idris
Code Reference : https://stackoverflow.com/a/3145655/28557
-----------------------Update-----------------------
Now Android have Fused location provider
The Fused Location Provider intelligently manages the underlying location technology and gives you the best location according to your needs. It simplifies ways for apps to get the user’s current location with improved accuracy and lower power usage
Fused location provider provide three ways to fetch location
Last Location: Use when you want to know current location once.
Request Location using Listener: Use when application is on screen / frontend and require continues location.
Request Location using Pending Intent: Use when application in background and require continues location.
References :
Official site : http://developer.android.com/google/play-services/location.html
Fused location provider example:
GIT : https://github.com/kpbird/fused-location-provider-example
http://blog.lemberg.co.uk/fused-location-provider
--------------------------------------------------------
GPS is generally more accurate than network but sometimes GPS is not available, therefore you might need to switch between the two.
A good start might be to look at the android dev site. They had a section dedicated to determining user location and it has all the code samples you need.
http://developer.android.com/guide/topics/location/obtaining-user-location.html
There are some great answers mentioned here. Another approach you could take would be to use some free SDKs available online like Atooma, tranql and Neura, that can be integrated with your Android application (it takes less than 20 min to integrate).
Along with giving you the accurate location of your user, it can also give you good insights about your user’s activities. Also, some of them consume less than 1% of your battery