Android 6.0 start Dynamic request permission

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/kjunchen/article/details/51366845

Dynamic Request Permission

Starting with Android 6.0 (API 23), users are allowed to decide whether to allow permissions while the app is running, rather than when the app is installed. This approach simplifies the application installation process because users do not need permission to install or update the app. He also gives users more control over the functionality of the app, for example, the user can choose to give the camera the right to apply the camera but not allow the device location. Users can revoke permissions at any time by entering the app settings.

system permissions are divided into two types, normal and sensitive (dangerous):

    • Normal permissions do not directly put the user's privacy at risk. If your app has normal permissions listed in the manifest file, these permissions are automatically allowed.

    • the sensitive permission gives the application the confidential data of the bearing user. If your app includes dangerous class permissions in the manifest file, it explicitly gives the user permission to your app.

For more information, see Normal and Dangerous Permissions.

In all Android versions, your app needs to declare the normal and dangerous permissions it needs in the manifest file. However, the impact of the claim is different, depending on the system version and the target SDK level you apply:

    • If the device is running on Android 5.1 or lower, or your app's target SDK is 22 or lower, if you add sensitive permissions to the manifest file, they must agree to the permissions when they install the app, and if they don't agree, the app won't install.

    • If the device is running on Android 6.0 or later, or your app's target SDK is 23 or higher. The app must include permissions in the manifest file, and must request every dangerous permission when it needs to run. Users can allow or deny each permission, even if the user rejects a request for a permission and the app can continue to run with limited functionality.

Note: starting with Android6.0 (API 23), users can revoke any app's permissions at any time, even if the app target SDK is below 23. You should test your use to verify that it behaves normally when it lacks the required permissions, regardless of the target SDK level of your app.

Check Permissions

If your app requires a sensitive permission, you must check to see if you have permissions when you need a permission every time you perform an operation. Users are always free to revoke permissions, so even though the app used the camera yesterday, it may not have camera rights today.

To check if you have this permission, call the contextcompat.checkselfpermission () method. For example, the following fragment shows how to check whether an activity has calendar permissions:

// Assume thisActivity is the current activityint permissionCheck = ContextCompat.checkSelfPermission(thisActivity,        Manifest.permission.WRITE_CALENDAR);

If a permission is used, this method returns packagemanager.permission_granted , and the app can continue to operate. If the app does not have permissions, the method returns permission_denied , and the app must go to the user requesting the permission.

Request Permission

If your app requires a sensitive permission to join the manifest file, he must ask permission from the user. Android provides some ways for you to request a permission. Calling these methods will pop up a standard Android pop-up box that you cannot customize.

explain why your app needs permissions

In some cases, you might want to help users understand why your app needs a permission. For example, if a user runs a camera app and the app requests to use camera permissions, the user is not surprised, but the user doesn't understand why the app wants to access the user's location or contact person. Before you ask for a permission, you should consider providing an explanation to the user. Remember, if you don't want to convince the user with an explanation, if you provide too much explanation, the user may find the app frustrating and uninstalling it.

If the user has closed the permission request, you may need to provide an explanation. If a user insists on trying to use a feature but needs to request a permission, but always closes the permission request, it may indicate that the user does not understand why the app needs the feature. In this case, it might be a good idea to show an explanation.

To help with this situation, the user may need an explanation that Android provides a practical way toShouldshowrequestpermissionrationale () . This method returns true if the app has previously requested this permission and was rejected by the user.

Note: This method returns false if the user requests the system popup in the permission request and chooses not to prompt. This method also returns falseif a device has disabled that permission for the app.

request the permissions you need

If your app does not have the permissions you need, you must call the requestpermissions () method to request the appropriate permissions. Your app passes a permission request, and an integer request code uniquely identifies the permission request. This method is asynchronous, and when the user operates the popup, he returns immediately, and the system invokes the application's callback method with the result.

The following code is to check if the app has permission to read the contact, and request permission if necessary:

//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 user is presented with a standard dialog box. You cannot configure or repair a dialog box. If you need to provide any information or explanation, you should do so by calling Requestpermissions () , just like explaining why your app needs a description in permissions.

Handling Permission Request Responses

When your app asks for permission, the system will want to show you a dialog box. When the user responds, the system invokes the Onrequestpermissionsresult () method that you applied. Your app must override this method to find out if permissions are allowed. This callback interface has the same request code as the requestpermissions () method. For example, if an app requests read_contacts Access, his callback method might look like this:

@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}}

The dialog box shows the permission groups described by the permissions that your app requires, and it does not list specific permissions. For example, if you request read_contacts permissions, the System dialog box simply says that your app needs to access the device's contacts. For each permission group the user only needs to be allowed once. If your app requests any other permissions in that group, the system will automatically allow them. When you request permission, if the user has explicitly allowed your request in the System dialog box, the system calls the Onrequestpermissionsresult () method and passes the permission_granted.

Note: your app still needs to explicitly request every permission it needs, even if the user has allowed another permission in the same group. In addition, group permission groupings may change in future versions of Android. Your code should not rely on specific permissions in a group.

For example, suppose you apply a manifest file that has both read_contacts permissions and write_contacts permissions. If you request read_contacts and the user allows permission to change, and then you request write_contacts , the system immediately allows permissions without interacting with the user.

If the user rejects a permission request, your app should take appropriate care. For example, your app might show a dialog box explaining why it needs permissions to perform the actions requested by the user.

When the system asks the user to allow permission, the user can choose to tell the system not to ask for permission here. In this case, any time the application uses the requestpermissions () method to request permission at the time, the system will immediately reject the request. If the user has explicitly rejected your request, the system calls the Onrequestpermissionsresult () method and passes the permission_denied . This means that when you call the requestpermissions () method, you cannot produce the desired effect where the user is operating.

Normal permissions (Permissions)

In API 23, the following permissions are defined as normal permissions

    • access_location_extra_commands
    • access_network_state
    • access_notification_policy
    • access_wifi_state
    • BLUETOOTH
    • Bluetooth_admin
    • Broadcast_sticky
    • change_network_state
    • change_wifi_multicast_state
    • change_wifi_state
    • Disable_keyguard
    • Expand_status_bar
    • get_package_size
    • Install_shortcut
    • INTERNET
    • kill_background_processes
    • modify_audio_settings
    • NFC
    • read_sync_settings
    • Read_sync_stats
    • receive_boot_completed
    • Reorder_tasks
    • request_ignore_battery_optimizations
    • request_install_packages
    • Set_alarm
    • Set_time_zone
    • Set_wallpaper
    • set_wallpaper_hints
    • Transmit_ir
    • Uninstall_shortcut
    • Use_fingerprint
    • Vibrate
    • Wake_lock
    • write_sync_settings
Sensitive permissions (dangerous permissions)
Permission Group Permissions
CALENDAR Read_calendar, Write_calendar
CAMERA CAMERA
CONTACTS Read_contacts, Write_contacts, get_accounts
Location Access_fine_location, Access_coarse_location
Microphone Record_audio
PHONE Read_phone_state, Call_phone Read_call_log, Write_call_log, Add_voicemail, Use_sip, Process_outgoing_calls
Sensors Body_sensors
Sms Send_sms receive_sms, read_sms Receive_wap_push, Receive_mms
STORAGE Read_external_storage, Write_external_storage
original link
    • Http://developer.android.com/training/permissions/requesting.html#perm-request

    • Http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

    • Http://developer.android.com/guide/topics/security/normal-permissions.html

My English ability is limited, translation is inevitably wrong, please include a lot of readers! also welcome correction.

Welcome to add QQ Group Exchange: 365532949
Homepage: http://junkchen.com

Android 6.0 start Dynamic request permission

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.