简体   繁体   中英

Marshmallow app-permission: android.location.PROVIDERS_CHANGED is not fired

I have an App that listens to android.location.PROVIDERS_CHANGED broadcast to stay aware of the phone's current location capabilities.

With Android M and the new App-runtime permissions, it works normally when the location is changed from the system/general panel. But if a user disables the location permission specifically for the App, the same broadcast is not fired.

So far I can test for location access with a regular AlarmManager, but it's quite odd and not very responsive.

Is there any other specific method, ideally a kind of BroadcastReceiver to register that keeps us informed of this location app-permission-change with Android M?

Sorry, but you are not directly notified about permission changes, for locations or other permissions, unless there's some undocumented hack that I'm not aware of.

If the user revokes a permission, your app's process will be terminated. The idea is that you will find out about the revoked permission when your app runs again and you call checkSelfPermission() . That flow is optimized for more conventional cases, where the permission checks are being conducted in an activity opened by the user. It doesn't handle your case very well (though, IMHO, location permissions shouldn't affect the broadcast as you describe).

In the N Developer Preview, you can now set up JobScheduler to monitor a Uri for changes and trigger your job as needed. This is a bit like registering a ContentObserver , except that you don't need the process running all the time — JobScheduler registers the observer and just invokes your JobService as needed. I think that the roster of enabled location sources is found in the Settings provider somewhere; if so, for Android 6.1/7.0/Turbo System 5000/whatever N turns into, you might be able to use JobScheduler to find out about the location source changes, instead of relying on the broadcast.

That doesn't help you for Android 6.0, though. You can use JobScheduler or AlarmManager or something to see if you lost the permission (via checkSelfPermission() ), but that's kinda wasteful of battery life (and, as a result, will not work well given Doze mode and possibly app standby).

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