Location Access in Android 11 - android

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.

Related

How do some application by pass the ACCESS_BACKGROUND_LOCATION even when they are using location service even when app killed?

From the android documentation, the ACCESS_BACKGROUND_LOCATION permission should be stated for app that access the background location, user should see the "Allow all the time" option in the permission page of the application.
Yet, there are a lot of sports app, say strava, that track the gps of user continuously, even when app killed or app in background. They didn't even state the permission of ACCESS_BACKGROUND_LOCATION. This can be verify when going to their app permission page, only allow only when using app, ask every time and refuse options are shown. I understand they use foreground service to do such thing, but how come they didn't state for the ACCESS_BACKGROUND_LOCATION permission? Isn't this a must for apps to track user in background as required by Google? Can anyone give me some explaination on this? Thanks

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

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.

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 :)

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.

Android Location from Startup

i am currently developing an app that requires location from the start. I have a method that checks if location updates are up, but even when GPS is turned up I cant ensure a Location Update when the app starts. How can I ensure a fresh location update when my app starts?
i am currently developing an app that requires location from the start.
That is a design flaw in your app. Please redesign your app to take into account that:
the user may have GPS disabled, or
the user may not have used GPS recently, so it will take some time to acquire a fix, or
the user may not be in a location where GPS signals are available (e.g., inside a large building)
How can I ensure a fresh location update when my app starts?
You can't.

Categories

Resources