13

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

Rupert
  • 2,097
  • 19
  • 28
  • Nothing like this exists, you can only check when you need it – tyczj Jul 13 '18 at 14:22
  • Methods you mention only return permissions of your own package, you cannot check other applications. – Pawel Jul 13 '18 at 14:24
  • 3
    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). – Michael Jul 13 '18 at 14:25
  • @Pawel 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). – Rupert Jul 13 '18 at 14:42
  • @Michael Thanks — good to know that at least the activity stack is cleared! – Rupert Jul 13 '18 at 14:43

2 Answers2

7

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 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… – Rupert Jul 13 '18 at 14:40
  • @Rupert: "but we have no way of monitoring for permission granting if it happens elsewhere in the app" -- correct. – CommonsWare Jul 13 '18 at 21:16
3

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.

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
py_n00b
  • 75
  • 1
  • 7