From sdk 23 Android requires runtime permissions for permissions on devices running Android 6.0 and higher, within what is classed as the Dangerous Permission Groups. Dangerous permission groups are one's that are considered to compromise the user's privacy and/or security.
The following is a list of Dangerous Permission Groups:
Dangerous Permission Groups
Any permissions from these groups requires management of runtime permissions for devices on Android 6.0 and higher with a target sdk of 23 or higher.
The following is a list of normal permissions. These are not regarded as dangerous to the user's privacy or security and so do not require runtime permissions for sdk 23 and higher.
This example shows how to check permissions at runtime in Android 6 and later.
Enforcing Permissions in Broadcasts, URI
You can do a permissions check when sending an Intent to a registered broadcast receiver. The permissions you send are cross-checked with the ones registered under the tag. They restrict who can send broadcasts to the associated receiver.
To send a broadcast request with permissions, specify the permission as a string in the Context.sendBroadcast(Intent intent, String permission) call, but keep in mind that the receiver's app MUST have that permission in order to receive your broadcast. The reciever should be installed first before the sender.
The method signature is:
and you can specify in your manifest that the broadcast sender is required to include the requested permission sent through the sendBroadcast:
Also declare the permission in the manifest of the application that is supposed to receive this broadcast:
Note: Both a receiver and a broadcaster can require a permission, and when this happens, both permission checks must pass for the Intent to be delivered to the associated target. The App that defines the permission should be installed first.
Include all permission-related code to an abstract base class and extend the activity of this base class to achieve cleaner/reusable code
Example usage in the activity
The activity should extend the abstract base class defined above as follows:
Multiple Runtime Permissions From Same Permission Groups
In the manifest we have fours dangerous runtime permissions from two groups.
In the activity where the permissions are required. Note it is important to check for permissions in any activity that requires permissions, as the permissions can be revoked while the app is in the background and the app will then crash.
We only need to ask for permission for one of these from each group and all other permissions from this group are granted unless the permission is revoked by the user.
This deals with the result of the user allowing or not allowing permissions. In this example, if the permissions are not allowed, the app is killed.
PermissionUtil is a simple and convenient way of asking for permissions in context. You can easily provide what should happen in case of all requested permissions granted (onAllGranted()), any request was denied (onAnyDenied()) or in case that a rational is needed (onRational()).
Anywhere in your AppCompatActivity or Fragment that you want to ask for user's permisssion
And add this to onRequestPermissionsResult
Add the requested permission to your AndroidManifest.xml as well
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0