Requesting permissions at run time
Starting with Android 6.0 (API level 23), user rights are granted to applications when they install programs while the application is running. This approach simplifies the application installation process because users do not need to grant permissions when installing or updating applications. This also gives the user more control over the application's functionality; For example, a user can choose to give the camera application access to the camera rather than the device's location. The user can revoke permissions at any time by setting the application screen.
System permissions fall into two categories, common and dangerous:
- Normal permissions are not directly user's privacy risk. If your application manifest lists a normal permission, the system automatically grants the license.
- Dangerous permissions can give an application access to the user's confidential data. If your application manifest lists a normal permission, the system automatically grants the license. If you list a dangerous license, the user must explicitly approve the application.
In all versions of the Android system, your application needs to declare the right and dangerous permissions required in its application manifest, as described in the declaration. However, the impact of the manifesto is different depending on the system version and the target level of the SDK application:
- If the device is running Android 5.1 or lower, or the target SDK for the application is 22 or lower: If you have a dangerous license in the list list, the user grants permission to install the application, and if they do not grant permission, the system does not have the application installed.
- If the device is running Android 6.0 or later, and your application's target SDK is 23 or higher: The list of permissions for the app listing, it must require that each dangerous permission needs to be run while the application is running. The user can grant or deny each permission, and the application can continue to run with limited capacity, even if the user denies the permission request.
Note : from Android 6.0 (API level 23), users can revoke permissions from any application at any time, even if the application API level is low. You should test your application to verify that it behaves correctly when you lose a need to license, regardless of the target of the API level application.
This lesson describes how to use the Android support library to check, and request, permissions. Android 6.0 's Android Framework provides a similar approach (API level of all). However, it is easier to use a support library because the application does not need to check which version of Android to run before calling the method.
Check Permissions
If your application requires a dangerous license, you must check whether you have permission to perform one operation at a time and ask for permission. The user can always revoke the license, so even if the camera used the application yesterday, it cannot assume that it still has this permission today.
Check if you have permission to call the contextcompat.checkselfpermission () method. For example, this snippet shows how to check write permissions on the active calendar:
// assume Thisactivity is the current activity int permissioncheck = contextcompat.checkselfpermission (thisactivity, Manifest.permission.WRITE_ CALENDAR);
If the application is licensed, the method returns Packagemanager. permission_granted, the application can continue to run. If the application does not have a license, the method returns permission_denied, and the application must explicitly require user permission.
The requested permission
If your app requires a risky license listed in an application manifest, it must require the user to grant permissions. Android offers several ways to request a license. Call these methods to present a standard Android dialog box that you cannot customize.
Explain why this application requires permissions
In some cases, you may want to help users understand why your app requires a license. For example, if a user launches an application, the user may not be surprised to allow the application to require the use of the camera, but the user may not understand why the application wants to access the user's location or contact. Before requesting permission, you should consider providing an explanation to the user. Keep in mind that you don't want to overwhelm users by providing explanations, and if you provide too much explanation, users may find apps frustrating and deleting it.
One way you can use this is to provide an explanation only if the user has rejected the permission request. If a user has been trying to use a feature that requires a license but has been rejecting the licensing requirement, this may indicate that the user does not understand why the application needs to be provided with that functionality. In such a case, it may be a good idea to give an explanation.
To help find the case, the user may need an explanation,Android provides a utiltity method ,Shouldshowrequestpermissionrationale (). This method returns trueif the application requires this permission before the user rejects the request.
Note : If the user rejects the license request in the past and again chooses the Do not ask option to allow the Request System dialog box, this method returns FALSE. The method also returns false if the device policy prohibits the application from owning the permission.
Required permissions for the request
If the application has not allowed its requirements, the application must call a requestpermissions () method to request the appropriate permissions. The application passes the permissions it wants, and an integer asks you to specify the code that recognizes this permission request. This method of asynchronous functions: it returns, after the user responds to the dialog box, the system invokes the application's callback method on the result of the effect, through the same request code, the application is passed to requestpermissions ().
The following code checks that the application allows the user to read the contact and, if necessary, request permission:
//Here , thisactivity are the current activityif(Contextcompat.checkselfpermission (thisactivity, Manifest.permission.READ_CONTACTS)!=packagemanager.permission_granted) { //should we show an explanation? if(Activitycompat.shouldshowrequestpermissionrationale (thisactivity, Manifest.permission.READ_CONTACTS)) { //Show an expanation to the user *asynchronously*--don ' t block//This thread waiting for the user ' s response! after the user//sees the explanation, try again to request the permission. } Else { //No explanation needed, we can request the permission.activitycompat.requestpermissions (thisactivity,Newstring[]{manifest.permission.read_contacts}, my_permissions_request_read_contacts); //My_permissions_request_read_contacts is an//app-defined INT constant. The callback method gets the//result of the request. }}
Note : When your application calls requestpermissions (), the system displays a standard dialog box to the user. The application cannot configure or change the dialog box. If you need to provide any information or explain to the user, you should do so before you call requestpermissions (), explaining why the application requires permissions.
Handling Permission Request Responses
When the application requests permissions, the system provides a dialog box to the user. When the user responds, the system invokes the application's Onrequestpermissionsresult () method, passing the user response. Your app must override that method to find out if a license is granted. The callback passes the same request code to requestpermissions (). For example, if an application requests read_contacts Access It may have the following callback method:
@Override Public voidOnrequestpermissionsresult (intRequestcode, String permissions[],int[] grantresults) { Switch(requestcode) { Casemy_permissions_request_read_contacts: {//If request is cancelled, the result of arrays is empty. if(Grantresults.length > 0 && grantresults[0] = =packagemanager.permission_granted) { //permission was granted, yay! do the//contacts-related task need to do. } Else { //Permission denied, boo! Disable the//functionality that depends in this permission. } return; } //Other ' case ' lines to check for other//permissions This app might request }}
The dialog box shown, which describes the system that allows your application to be accessed by the organization, does not have a specific list of permissions. For example, if you request a read_contacts license, the System dialog box simply says that your application needs to access the device's contacts. Once for each user only need to license permission group. If your application requests any additional permission groups (listed in the application manifest), the system automatically assigns them. When requesting permission, the system calls your Onrequestpermissionsresult () callback method and passes the permission_granted, in the same manner as if the user has explicitly granted your request through the System dialog box.
Note : Applications still require each license to be explicitly requested, even if the user has already allowed another in the same group. In addition, grouping of permissions may change the future version of Android. Your code should not depend on assuming specific permissions or not in the same group.
For example, assume that you have a list of read_contacts and write_contacts in the application. If you request read_contacts and the user to grant permissions and then you request write_contacts, the system immediately grants permission to not interact with the user.
If the user rejects the permission request, the application should take appropriate action. For example, your application might display a dialog box explaining why it cannot perform the action requested by the user and requires permission.
When the system asks the user to grant permission, the user can choose to tell the system not to ask for permission. In this case, any time an application uses requestpermissions () requires permission, the system immediately denies the request. The system calls your Onrequestpermissionsresult () callback method and passes the permission_denied in the same way if the user has explicitly rejected your request. This means that when you call requestpermissions (), you cannot assume any direct interaction with the user.
Android 6.0 run-time permission handling