Is it OK to retrieve cellular location when location setting is OFF? - android

On Android, we can retrieve CellLocation using this API
This API requires location(Coarse or Fine) permission to be granted.
We observed that API returns valid CellLocation even if global location-setting is OFF. Ideally, it should not give cell-location when location-setting is OFF.
Is the current API behavior intentional? Is it a bug in Android? Is it OK to access CellularLocation even if location setting is OFF?

There are two separate systems here:
App permissions
User toggles
If an app received the Location permission, it's allowed to use the API to get location whenever and however is wants (you should obviously take battery and data usage in consideration, but that's up to you).
If a certain settings toggle is switched on or off by the user, it's up to the API to turn off certain features from apps, it's not the app's responsibility to check system settings whenever it wants to use an API.
In your case the location setting can be toggled by the user to prevent usage of the GPS component to prevent accurate location tracking and to save battery.
This does not apply to cell-tower location as it's definitely not accurate and the data already exists on the phone so no battery power is needed either.
Android may in the future turn off cell-tower location when this setting is off, but until then you're allowed to use whatever the API gives you.

Related

Location Access in Android 11

Google documentation states:
If you request foreground
location and background location
at the same time, the system throws an exception.
Now, suppose I want my app to be able to access location when (i) the device is locked (ii) the device is unlocked and the app is in foreground. Is it possible in this scenario?
According to the documentation page here:
Even if several features in your app require location access, it's
likely that only some of them require background location access.
Therefore, it's recommended that your app performs incremental
requests for location permissions, asking for foreground location
access and then background location access. By performing incremental
requests, you give users more control and transparency because they
can better understand which features in your app need background
location access.
The process for performing incremental requests is as follows:
At first, your app should guide users to the features that require
foreground location access, such as the "share location" feature in
Figure 1 or the "show current location" feature in Figure 2.
It's recommended that you disable user access to features that require
background location access until your app has foreground location
access.
At a later time, when the user explores functionality that requires
background location access, you can request background location
access.
So the short answer is yes. You can use both foreground and background locations in the app, just don't ask for both at the same time.

Handle permissions change while in app

I am having a hard time understanding the right way to handle a user changing a permission while my app is still running in the background.
In my app I have a location class that registers for location changes and when the location changes the status is sent to a server. However this runs in the background.
When my app is launched I check with the user if its ok to use location services and if so I proceed with setting up that class. However the user can background my app and go into settings and remove that permission. I can, and will certainly check that the permission is enabled in my location class before asking for a location from the location service to avoid a crash. However I am not in an activity when a location comes in so I am not sure how to prompt them that my app needs location services.
EDIT:
It does seem that android restarts your app if a permission has been revoked in settings. However I have confirmed that as of now android does NOT restart your app if a permission was granted though settings.
I read somewhere that your app gets killed when the user changes the permissions on Android-M so you can be sure that this won't change while your app is running. It will been killed when this changes.
As reference check this video: https://www.youtube.com/watch?v=f17qe9vZ8RM
However I am not in an activity when a location comes in so I am not sure how to prompt them that my app needs location services.
Raise a Notification, alerting the user that your app cannot do its intended work without the permission that they revoked. Have the Notification tie into an Activity via a PendingIntent where the user will be able to grant that permission.
Along with CommonsWare suggestion, you can have the onProviderDisabled() to know which provider (gps, network) has been disabled and accordingly requestLocationUpdate() for the one that is still enabled. If both are disabled, see if at least Cell Location is of useful for your app. If so, you can send Cell Location at least till user see notification and re-enable the permission.Use PhoneStateListener to do that.
I would like to try a more modern 2020+ answer to the core question:
However I am not in an activity when a location comes in so I am not sure how to prompt them that my app needs location services.
However I am not in an activity when a location comes in so I am not sure how to prompt them that my app needs location services.
If you are in a normal end user environment:
Respect the users choice to revoke the permission and only display the missing permissions to the user if she opens your activity.
On modern devices your service needs to display a notification in the bar to even be allowed to continue running - change the notification to show the problem.
You are allowed to just ask for most permissions but the user has the ability to deny on the 2nd attempt. After that you get auto-no without anything displayed.
Some permissions (e.g. write settings and overlay) can be accessed by opening the settingspages for this directly - which can be done from a service but will be seen as harassment.
If you are in a work environment:
Best use an official mdm solution (COPE).There you can totally zombiefy your devices allowing nothing or anything and pretty much anything in between. User cannot even enter settings if you dissallow or not even turn the device off or.. you name it.
And apps can get all permissions they need and be installed automatically from the getgo.
For both (eben in mdm sometimes a more powerful user might be wanted):
Please build an extra Activity or Fragment (if you have one that uses those) dedicated to display why your app needs a permission and a button for the user to initiate the request/opening of settings.
It may be much work but users and google will be happy :)

How often does the GPS check for location changes

How often does the GPS check for location changes without being specifically asked with requestLocationUpdates? Does that change when the screen is off?
My app just sets up a proximity alert, without any background service running, and it plays a sound when entering the proximity. I'm trying to find out the limitations to this approach, if there are any.
As long as the GPS chip is enabled it sends 1 location per second to the API.

Android GPS battery usage

I am writing a program in Android that uses GPS. It will prompt the user to turn on location services if it is not on (which I guess is equivalent to turning on gps ?). My question is: does the gps immediately start consuming extra battery power as soon as location services is turned on or it uses batter power whenever the location services is queried/used for a new location ?
The GPS starts working only when the location manager requests an update from it. Turning on the GPS in the device only allows programs to request locations if they want, but it doesn't mean the GPS is kicking in.
You can test it yourself: The GPS icon in the notification bar tells the GPS status. You can turn on the GPS, but the icon won't appear. Why? No application requests updates now. As soon as an application requests an update, the icon starts blinking (The GPS is initializing itself) and when it stops, it means the GPS is initialized and is currently receiveng updates.
The GPS consumes battery only when this icon appears.
My company wrote a blog post on this subject a while back. Check to see if its useful.
http://www.littleeye.co/blog/2013/03/29/understanding-gps-resource-usage/
But we missed talking about what happens when no app is actually requesting for a location update. And yes, as mentioned unless apps actually ask for a location update, they will not cause power drain. But note that even a single request for a location update (depending on coarse or fine grained request) can have unintended effects, as the device has to go thro various states before it can get back to its steady state.
Turning on location services is just permission given by user to applications for using location services. As long as any application not asks for location, power consumption is the same as with location sesrvices turnded on.
On the other hand - there is many applications trying to get location. Facebook, Camera, Android, many of ads libs, so yes - user can get battery life issues when GPS is unlocked.

Retrieving GPS coordinates when location services are not enabled

Assuming the device's "Use Wireless networks" and "Use GPS satellites" settings under Location is not enabled, is it possible to still retrieve the location coordinates.
Currently, the best is to redirect the user to the settings page is not enabled. I find this disruptive as needs to navigate away from the app.
If i am not mistaken, you can enable the settings programmatically (Am I correct?).
I don't need the exact coordinates, is it possible to get the cell tower ID without the settings enabled?
No. If the user doesn't want to give you his location, There is no way you can get his location.
This is a security measure and it exists for good reasons. If you try to subvert this, your app will be considered malicious.
Redirecting the user to the settings page is the best option.
Agreed - in earlier versions of android, you could programatically turn on the GPS - but more recent versions do not allow this to happen.
As Anup states - the common acceptable practice now is to redirect the user to the options page and allow the user to select the type and nature of GPS that the application is allowed to use.
Dont forget , you dont need to "leave the app" to get the user to turn on GPS, you can send them to the settings yourself:
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
This will then come back to your app where it left off - giveing the use a seamless experience, and one which they will be familiar with from other android apps.
Setting anything programatically without asking the user first is bad practice.
What you could do is, in case you can't get a location, ask the user to turn GPS on or tell him your app won't be able to work and will close.
switching on Location settings programmatically without the users consent is impossible let alone bad practice. You can prompt the user and direct him to switch on location settings or if you program tracks the progress of the device user, you can have the program send an alert of some sort telling them that the Location setting is off.
Deep dive into location has a set of best practices for Android's LocationManager. You do not need the GPS to be activated -- in many cases, the mobile network itself gives a pretty good approximation of the location without taking up extra battery.

Categories

Resources