lostintranslation
lostintranslation

Reputation: 24563

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.

Upvotes: 13

Views: 8161

Answers (4)

FrankKrumnow
FrankKrumnow

Reputation: 508

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

Upvotes: 0

rekire
rekire

Reputation: 47945

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

Upvotes: 6

cgr
cgr

Reputation: 4636

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.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006594

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.

Upvotes: 2

Related Questions