Android Dynamic permission request

Source: Internet
Author: User

Starting with Android 6.0, permissions are no longer pasted in the manifest file, this time the authority also formally into everyone's vision. The 6.0 adaptation of the project is what I did, did not carefully summarize, recently in another project added permissions to find that the same feature does not add the code to request permission, one will hang a won't, spend a few hours on this small problem. So spend a bit more time summarizing the permissions issue.

    1. The concept of Android system permissions

Android is a privilege-delimited operating system, with unique system identities for each application. In general, apps do not have permissions to perform operations that may adversely affect other applications, systems, or users. Each app runs in the app sandbox, so when apps need to use features not provided by the sandbox, they need to request permissions such as reading and writing SD cards, accessing the network, accessing data from other apps, reading and writing contacts, calling the camera, and so on.

Permissions in the Androidmanifest.xml file, before Android 6.0, some apps a brain declared a variety of permissions, users may not have to look at the installation, so these apps can do whatever they like, sly, Lawless. Android 6.0 divides permissions into normal and dangerous permissions, and the normal permissions declared in Androidmanifest are automatically granted, while dangerous permissions need to be explicitly granted by the user at the time of use.

In other words, Android 6.0 or more of the system in the first use of dangerous rights, you need to apply to the user, with the consent of the user. If you do this without permission, you will get the crash package, which is the error log java.lang.SecurityException: Permission Denial . Therefore, application for dangerous permission needs to be dealt with accordingly.

    1. Dangerous permissions and corresponding permission groups

Dangerous permissions belong to the permission group, when applied to the user to apply for dangerous permissions, the system will play a dialog box describing the permission group to be accessed by the application, if the user agrees to authorize, then the permission group contains all the permissions will be granted by the system. For example, once the app is granted Read_external_storage permissions, the permission is granted immediately if you request Write_external_storage permissions again.

The hazard permission form is as follows

tr>
permission Group permissions
CALENDAR read_ CALENDAR

Write_calendar
CAMERA CAMERA
contacts read_contacts

write_contacts

get_accounts
location Access_f Ine_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_mm S
STORAGE read_external_storage

write_external_storage

3. Correct posture to apply for permission
* All permissions required for application are stated in the Androidmanifest file

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" / >
  • Check permissions when using, apply without permission
    //using a compatibility library eliminates the need to determine the system version        intHaswritestoragepermission =contextcompat.checkselfpermission (Getapplication (), Manifest.permission.WRITE_EXTERNAL_STORAGE); if(Haswritestoragepermission = =packagemanager.permission_granted) {                        //have permissions to perform actionsInitscan (); }Else{                        //no permissions, request permission from userActivitycompat.requestpermissions (Thisactivity,Newstring[]{manifest.permission.write_external_storage}, Myapplication.code_for_write_permission); }
  • Overwrite Onrequestpermissionsresult method
    @Override Public voidOnrequestpermissionsresult (intRequestcode, string[] permissions,int[] grantresults) {                //use Requestcode to identify whether the same request        if(Requestcode = =code_for_write_permission) {            if(Grantresults.length >0&& grantresults[0] ==packagemanager.permission_granted) {                //user agrees to perform the actionInitscan (); }Else{                //user does not agree to show the user the role of this permission                if(Activitycompat.shouldshowrequestpermissionrationale ( This, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {                    NewAlertdialog.builder (thisactivity). Setmessage (R.string. Storage_permissions_remind). Setpositivebutton ("OK", (Dialog1, which)Activitycompat.requestpermissions ( This,                                            NewString[]{manifest.permission.write_external_storage}, Eventconstconfig.cod e_for_camera_permission)). Setnegativebutton ("Cancel",NULL). Create (). Show (); }            }        }    }

    The return value of the Shouldshowrequestpermissionrationale method is divided into several cases, how to use to see the specific interaction needs of the application.
    1. Request this permission for the first time and return false.
    2. The permission was requested and rejected by the user, returning True.
    3. The permission is requested, but the user refuses to check the reminder again, return false.

      1. A few small details about requesting permission

        • Using the Compatibility library
          Checkselfpermission, Requestpermissions and several other rights-related methods with the V4 package can be compatible with the following version of 6.0, or need to package a version of the judgment.

        • Targetsdkversion's Problem
          That's the problem I'm having, and there's a detail I didn't notice. There are actually two conditions for the Android system to trigger the dynamic request permission, the device system version is above Android 6.0 and targetsdkversion>=23. So in fact, in the case of Targetsdkversion version less than 23, even if more than 6.0 of the device will not be hung, but will be listed at the time of installation of all permissions, with 6.0 or less devices. The official proposal to keep Targetsdkversion in the latest version.

      2. Using third-party libraries Easypermissions
        Permission to apply for the third-party library has many, but did not encounter particularly concise, also did not specifically to research and use, to avoid importing too many third-party libraries, easypermissions This library is one of the projects used, here simple mention.

        • Import Easypermissions
    ‘pub.devrel:easypermissions:0.1.9‘
  • Check permissions, do not apply
    if (! Easypermissions.haspermissions (this, Manifest.permission.CAMERA)) {            Easypermissions.requestpermissions (This, getString (R.string. Camera_peemission_tip), Camera_request_code, Manifest.permission.CAMERA);        }
  • Overwrite method
    @Override Public voidOnrequestpermissionsresult (intRequestcode, string[] permissions,int[] grantresults)        {Super.onrequestpermissionsresult (Requestcode, permissions, grantresults); //Forward results to easypermissionsEasypermissions.onrequestpermissionsresult (Requestcode, permissions, Grantresults, This); } @Override Public voidOnpermissionsgranted (intRequestcode, list<string>perms) {//Light ();} @Override Public voidOnpermissionsdenied (intRequestcode, list<string>perms) {Dialog Dialog=NewAlertdialog.builder ( This, R.style.myalertdialogstyle). Settitle (R.string. Tips). Setmessage (R.string. Camera_peemission_tip). Setpositivebutton (R.string. To_open, (DIALOG2, which){startactivity (NewIntent (settings.action_application_settings)); }). Setnegativebutton (R.string. Cancel, (Dialog3, which),{Dialog3.dismiss ();        }). Create ();    Dialog.show (); }

                             ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                                                   from: http://blog.csdn.net/forevercbb/article/details/79131740& nbsp

Android Dynamic permission request

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.