Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 455
Rupert Avatar asked Jul 13 '18 14:07

Rupert


People also ask

Which permission is required to get access to a location in Android?

Location permissions. If your app needs to access the user's location, you must request permission by adding the relevant Android location permissions to your app. Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION .

How do I set location permissions on Android?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.

How do you check location is on or off in Android programmatically?

Checking for location manager: lm. getAllProviders(). contains(LocationManager. GPS_PROVIDER) (or NETWORK_PROVIDER ) would make sure that you do not throw the user to a settings page where there is no network option.


1 Answers

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.

like image 138
CommonsWare Avatar answered Sep 19 '22 23:09

CommonsWare