简体   繁体   中英

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM