Original Google
Original address: http://developer.android.com/intl/zh-cn/training/permissions/requesting.html
Original copyright: Creative Commons 2.5 Attribution License
Jianan-[email protected]
Version information: This article is based on the 2016-04-27 version of translation
Copyright: CC by-nc-nd 4.0, copy reproduced, but must retain the author's signature and translation links, not deductive and for commercial purposes
Objective
Starting with Android 6.0 (API 23), installing apps does not require authorization for app permission requests, but when the app is running, users need to authorize the app. This process streamlines the installation of the app, and users do not need to be authorized to install or upgrade the app. This also gives the user more control over the app's functionality; For example, a user can choose to give a camera app access to a webcam, but not access the device's geographic location. Users can also revoke permissions granted to the app at any time via the app's settings screen.
System permissions can be divided into two categories, ordinary permissions and dangerous permissions:
- Normal permissions do not cause a risk of user privacy breaches. If your app lists a normal permission in the manifest file, the system will automatically authorize the normal permissions of the application.
- Dangerous permissions allow the app to access the user's confidential information. If your app lists a normal permission in its manifest, the system will automatically authorize it. But if you list a dangerous permission, the user must explicitly deal with whether or not to authorize your app.
For more information, please refer to normal and dangerous permissions.
Regardless of which Android version you have, your app must declare all of the normal and dangerous permissions it needs on its manifest file, for more information, see declaring permissions. However, the result of declaring permissions differs depending on the system version and the app's target SDK:
- If the device is running on an Android 5.1 or earlier system, or the app's target SDK is 22 or lower: If your app declares a dangerous permission in its manifest file, Then the user must authorize this dangerous permission to your app when installing the app, and if the user does not agree to grant this permission, the system will refuse to install the app.
- If the device is running on an Android 6.0 or higher system version, and the app's target SDK is 23 or higher: The app must declare all the required permissions in the manifest file. And the user must be asked to authorize each dangerous permission to use during the app's run. Users can choose to authorize or deny each permission, if the user refused authorization, the app can continue to run, but the corresponding function is not available.
Note: Starting with Android 6.0 (API 23), regardless of whether the app's target SDK is a lower version of the SDK, users can cancel any permission that the app has already authorized at any time. No matter what the target SDK version of your app is, you should test whether the app is functioning properly when your app lacks a required permission.
This section explores how to use the Android support library to check and request permissions. The Android Framework provides a similar approach on Android 6.0 (API 23). However, it is relatively straightforward to use the support library because your app does not need to check the current version of the Android system that is running before calling these methods.
Check Permissions
If your app requires a dangerous permission, you must check to see if you have requested this permission every time you perform an action that requires this dangerous permission. Users can revoke granted permissions at any time, so even though an app can use the camera yesterday, it doesn't mean that it still has access to the camera today.
Check if your app has a permission and need to call the Contextcompat.checkselfpermission () method. For example, the following code snippet checks to see if activity has permission to write a calendar:
// Assume thisActivity is the current activityint permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR);
If the app has this permission, the method returns Packagemanager.permission_granted,app to continue with the action. If the app does not have this permission, the method returns Permission_denied,app needs to explicitly request that the user grant the permission.
Request permission
If your app needs to use a dangerous permission declared in its manifest file, it must ask the user to grant the permission before using it. Android offers a number of ways for you to apply for permissions. Requesting these methods will evoke a standard Android dialog box that you cannot customize.
Explain why apps need permissions
In some cases, you may want to help users understand why your app requires a permission. For example, if a user launches a photography app, the user should not be surprised that the app requests permission to use the camera, but the user may not understand why the app also requires access to the device's geographic location and contacts. Before you apply for a permission, you should consider providing an explanation to the user. But be careful not to use the explanation too much to harass the user, and if you provide too many explanations, the user may be bored with your app and uninstalled.
You may need to provide an explanation to the user only if the user has denied permission requests. If a user continues to try to use a feature that requires permission, but this permission has been rejected by the user, it means that the user may not be sure why the app's feature requires permission. In such cases, it is a good solution to display a permission request explanation.
To help determine under what circumstances a user needs a permission explanation, Android provides a tool method, Activitycompat.shouldshowrequestpermissionrationale (). This method returns true if the app has previously requested a permission, but the permission was rejected by the user.
Note: This method also returns False if the user has previously denied permission requests and has checked the no longer ask option in the System Permission Request dialog box. If the permissions that the app uses are disabled in the device permissions settings, this method will also return FALSE if the permission is used.
Apply the permissions you need
If your app does not currently have the permissions it needs, the app must call Requestpermissions () one of several overloaded methods to request the appropriate permissions. In these methods, your app needs to pass an integer value as an identifier for this permission request in addition to passing the required permissions as parameters. These methods are all asynchronous: The method returns directly and waits for the user to respond to the dialog box, and the system will callback the app's callback method to pass the result back, including the request code passed in when the app calls the Requestpermissions () method.
The following code checks to see if the app has permission to read the user's contacts, and if not, applies permissions when needed:
//Here, thisactivity are the current activityif(Contextcompat.checkselfpermission (thisactivity, Manifest.permission.READ_CONTACTS)! = Packagemanage r.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 app calls the Requestpermissions () method, the system pops up a standard dialog box to the user. Your app cannot configure or modify this dialog box. If you need to provide some information or explanation to the user, you should do so before calling the Requestpermissions () method, please refer to the section above to explain why the app needs permissions .
Handling the response of a permission request
When your app requests permissions, a dialog box is displayed to the user. When the user responds, the system will call back to your app's Onrequestpermissionsresulet () method to pass the response. Your app must override this method to determine whether the requested permission has been granted. The callback method receives the same request code that you passed to the requestpermissions () method. For example, if an app requests read_contacts access, it may need the following response code:
@Override Public void Onrequestpermissionsresult(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 you need.}Else{//Permission denied, boo! Disable the //functionality that depends on the this permission.}return; }//Other ' case ' lines to check for other //Permissions This app might request}}
This system displays a dialog box that describes the permission groups your app needs to access, and does not list which permissions to use. For example, if you request Read_contacts permissions, the System dialog box will only describe your app's need to access the device's address book. Users only need to authorize each permission group once. Once authorized for a permission group, these permissions are automatically granted if your app requests other permissions from the same permission group (also in the app's manifest file declaration). When you request additional permissions from these same permission groups, the system will also call back your Onrequestpermissionsresult () method and pass the permission_granted parameter. The result of the callback is the same as the user has explicitly consented to your permission request through the System dialog box.
Note: your app still needs to explicitly request every permission it needs, but the user has agreed to authorize other permissions in the same permission group. In addition, each permission group contains permissions that may change in future releases of Android. Your code logic should not be based on the assumption that several of the permissions used are included in the same permission group.
For example, suppose you declare read_contacts and Write_contacts permissions in the app's manifest. If you have already requested the Read_contacts permission and the user requests by authorization, when you request write_contacts permission again, the system will immediately authorize the permission to the app, no need to interact with the user for authorization.
If the user rejects your permission request, your app should take the appropriate response action. For example, your app might need to display a dialog box explaining why your app can't continue because it needs to be denied by the user.
When the system requests user authorization, the user can choose to tell the system to request this permission again. In this case, no matter how many times the app calls the Requestpermissions () method to request permission, the system will simply refuse to apply. The system will callback your Onrequestpermissionsresult () callback method and pass the permission_denied parameter value, the whole callback process is the same as the user has explicitly denied your permission to apply. This means that when you call the Requestpermissions () method, you cannot assume that an operation that interacts directly with the user will necessarily occur.
App requests permissions during Android 6.0 or above platform version run