Android6.0------Permission Request Management (single permission and multiple permission requests)

Source: Internet
Author: User

Android Development, after the 6.0 system, some permissions will have to apply for the use of.

Android divides permissions into normal permissions and dangerous permissions

Android system permissions are divided into several protection levels. The two most important levels of protection you need to know are normal permissions and dangerous permissions:

(1) Normal permission:

Covers areas where your app needs to access its sandbox external data or resources, but is less risky for user privacy or other application operations.

These permissions are granted when the app is installed, and the runtime no longer asks the user. For example: Network access, WiFi status, volume settings, and so on.

(2) Dangerous rights:

Covers areas where the application requires data or resources that involve user privacy information, or that may affect the operations of the user's stored data or other applications.

For example: Reading contacts, reading and writing memory data, obtaining user location, etc. If application claims require these dangerous permissions, you must explicitly tell the user at run time to have the user grant it manually.

Permission related knowledge, permission table See blog: Android6.0------Rights Management

Premise: The app is running on Android 6.0 (API level 23) a higher-level device, and the targetSdkVersion>=23 system will automatically adopt a dynamic rights management policy,

First Take a look: ( Note: If not authorized to click on the phone or take a photo will be directly back, so 6.0 must be manually authorized, development if not authorized, you can judge and prompt the user from the new authorization )

    

1: Single authorization, phone authorization.

2: There is a phone, SD card, photo authorization three together authorized

Case code:

 Public classMainactivityextendsAppcompatactivityImplementsview.onclicklistener{Private Static Final intMy_permissions_request_call_phone = 1; Private Static Final intMy_permissions_request_call_camera = 2; string[] Permissions=Newstring[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Ma    Nifest.permission.CALL_PHONE}; //declares a collection that is used in subsequent code to store the user's right to deny authorizationList<string> mpermissionlist =NewArraylist<>(); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview (); }    Private voidInitview () {Findviewbyid (R.ID.BTN1). Setonclicklistener ( This); Findviewbyid (R.ID.BTN2). Setonclicklistener ( This); Findviewbyid (R.ID.BTN3). Setonclicklistener ( This); Findviewbyid (R.ID.BTN4). Setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.btn1: // single authorization                   //Check if version is greater than M                if(Build.VERSION.SDK_INT >=Build.version_codes. M) {if(Contextcompat.checkselfpermission ( This, Manifest.permission.CALL_PHONE)! =packagemanager.permission_granted) {Activitycompat.requestpermissions ( This,                                NewString[]{manifest.permission.call_phone}, My_permissions_request_call_phone); }Else{showtoast ("Permission has been applied"); }                }                 Break;  Caser.id.btn2://More than one authorized  mpermissionlist.clear ();  for(inti = 0; i < permissions.length; i++) {                    if(Contextcompat.checkselfpermission (mainactivity. This, permissions[i])! =packagemanager.permission_granted)                    {Mpermissionlist.add (permissions[i]); }                }                if(Mpermissionlist.isempty ()) {//the permissions that are not granted are empty, and the representation is grantedToast.maketext (mainactivity. This, "authorized", Toast.length_long). Show (); } Else{//Request Permission Methodstring[] Permissions = Mpermissionlist.toarray (NewString[mpermissionlist.size ()]);//Convert list to arrayActivitycompat.requestpermissions (mainactivity. This, permissions, My_permissions_request_call_camera); }                 Break;  Caser.id.btn3:intent Camera=NewIntent (mediastore.action_image_capture); Startactivityforresult (Camera,1);  Break;  Caser.id.btn4:intent Intent=NewIntent (Intent.action_call); Uri Data= Uri.parse ("Tel:" + "10086");                Intent.setdata (data);                StartActivity (Intent);  Break; }} @Override Public voidOnrequestpermissionsresult (intRequestcode, string[] permissions,int[] grantresults) {        if(Requestcode = =My_permissions_request_call_phone) {            if(Grantresults[0] = =packagemanager.permission_granted) {Showtoast ("Permission has been applied"); } Else{showtoast ("Permission Denied"); }        }Else if(Requestcode = =My_permissions_request_call_camera) {                 for(inti = 0; i < grantresults.length; i++) {                    if(Grantresults[i]! =packagemanager.permission_granted) {                        //decide whether to stop asking after the ban is checked                        BooleanShowrequestpermission = Activitycompat.shouldshowrequestpermissionrationale (mainactivity. This, Permissions[i]); if(showrequestpermission) {showtoast ("Permission not Applied"); }                    }                }        }        Super. Onrequestpermissionsresult (Requestcode, permissions, grantresults); }    Private voidshowtoast (String string) {Toast.maketext (mainactivity. This, String,toast.length_long). Show (); }}

The premise must be noted: Androidmanifest:

  <uses-permissionAndroid:name= "Android.permission.CALL_PHONE"/>  // telephone     <uses-permissionAndroid:name= "Android.permission.CAMERA"/>  // photo     <uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>  //SD card   

This case is all written in Java code, the project is recommended to use less dangerous permissions, more words on their own encapsulation or the use of third-party.

Permission application There are a lot of third-party packaged libraries (tools) can be implemented, GitHub on a lot, follow-up blog will find a few better to explain.

Cases Click to download

Android6.0------Permission Request Management (single permission and multiple permission requests)

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.