How can we listen for location permission changes in Android (without triggering a permission request)?
Asked Answered
S

2

15

Is it even possible? I would like to be able to create a listener which will be notified about location permission changes (whether the app triggers them or not). As far as I can see, there are methods for getting the current permission status and methods for requesting permission but nothing which would simply allow the app to listen for changes.

For example, in iOS, we can set a delegate on a CLLocationManager which will then be called via the locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) on any change in authorisation status. Does anyone know of any equivalent in Android (preferably compatible with API >= 17)?

Strangulate answered 13/7, 2018 at 14:20 Comment(5)
Nothing like this exists, you can only check when you need itSimpleminded
Methods you mention only return permissions of your own package, you cannot check other applications.Fomentation
Also note that if your app has the Location permission, and the user goes into the Settings app and disables the Location permission for your app, Android will kill your app if it was running in the background (I don't know if it kills the process, but the activity stack is cleared anyway).Cliffcliffes
@Fomentation Don't worry — I wasn't concerned about monitoring location permissions for other app. However, it would be nice to be able to listen to global enabling/disabling of location status (I know that this is potentially a separate subject).Strangulate
@Cliffcliffes Thanks — good to know that at least the activity stack is cleared!Strangulate
I
8

Is it even possible?

No, sorry.

As far as I can see, there are methods for getting the current permission status and methods for requesting permission but nothing which would simply allow the app to listen for changes.

If the user grants you permission, the only way that you find out is if you call checkSelfPermission() again.

If the user revokes a previously-granted permission, as Michael suggests in a comment, your process is terminated, and you would find out about the permission change by calling checkSelfPermission() the next time your app runs.

Interstice answered 13/7, 2018 at 14:30 Comment(2)
Thanks for the reply. So it looks like it should be ok to monitor for revocation by checking on every activity creation (since revoking will destroy the activity), but we have no way of monitoring for permission granting if it happens elsewhere in the app…Strangulate
@Rupert: "but we have no way of monitoring for permission granting if it happens elsewhere in the app" -- correct.Interstice
J
5

There is no explicit way to listen for location permission changes. However, When I was developing a custom view which had a different behaviour when location permissions were there, I registered for activity lifecycle callback within the view and I will check for location permissions when onResume for relevant activity is triggered.

Generally, the consumers of this custom view should request for location permissions themselves. When permission request popup opens up and user select 'Allow' or 'Deny', onResume of your parent activity is triggered. This in turn will trigger your activity lifecycle callback and you can check the location permission status there.

Be careful not to leak activity instances through this lifecycle callback though.

Jodee answered 22/6, 2020 at 7:31 Comment(1)
onResume check - worked for my use case - ++++Ingravescent

© 2022 - 2024 — McMap. All rights reserved.