Talk about dynamic rights management for Android 6.0

Source: Internet
Author: User

1. Preface

As you all know, one of the new features of Android 6.0 is the management of application permissions. In other words, users can set up their own management if they are concerned with the user's privacy. However, before 6.0 , we installed an app that agreed to all of the permissions required by the app (such as location, access to a newsletter) and could not be installed without consent. Of course, some of the domestic handset manufacturers based on the Android customization system, can be implemented before 6.0 to close the specified permissions. Such as:

2. List of dangerous rights (dangerous Permission)

Dangerous permission are generally rights that involve user privacy.

From the above picture can be seen, the camera, telephone, positioning and so on are our usual development of the common permissions.

3. Can I apply rights Management in 6.0?

The answer is yes, but not recommended.

First of all, that's how inappropriate, that is, set targetsdkversion less than (Android 6.0 system default is targetsdkversion less than 23 of the app is granted all the permissions requested by default, So if your app sets a targetsdkversion below 23, it won't crash at run time. )

It's not quite good to have someone look at it, to solve the problem. So I want to tell you, first of all, this is not a permanent, sooner or later to face. You can't always targetsdkversion below 23. Second, it is a premise, that is, users do not operate their own permissions. To know if the user is more than 6.0 of the phone, he can turn off the permissions in the settings, then when the app because there is no permission to get data exceptions, resulting in a null pointer exception, the app will crash.

4. How to Fit

First Android Studio:

Declare targetsdkversion to be 23 and above in Build.gradle.

Eclipse:

Declare targetsdkversion to be 23 and above in Androidmanifest.xml.

This refers to the Checkpermissionsactivity class of the demo, the code is as follows:

/** * Inherits activity and implements ANDROID6.0 runtime permission detection * The activity that requires runtime permission detection can inherit this class * @ Created: May 27, 2016 3:01:31 * Project name: Amaploc Ationdemo * @author Hongming.wang * @ file name: Permissionschecker.java * @ type name: Permissionschecker * @since 2.5.0 * * Public  class checkpermissionsactivity extends Activity implements  Activitycompat. Onrequestpermissionsresultcallback {                        /** * Array of permissions required for detection */    protectedString[] Needpermissions = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_ Fine_location, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE , Manifest.permission.READ_PHONE_STATE};Private Static Final intPermisson_requestcode =0;/** * Determine if detection is required to prevent non-stop bouncing frames */    Private BooleanIsneedcheck =true;@Override    protected void Onresume() {Super. Onresume ();if(Isneedcheck)        {checkpermissions (needpermissions); }    }/** * * @param needrequestpermissonlist * @since 2.5.0 * Requestpermissions method is to request a right Limit, * /    Private void checkpermissions(String ... permissions) {list<string> needrequestpermissonlist = finddeniedpermissions (permissions);if(NULL! = needrequestpermissonlist && needrequestpermissonlist.size () >0) {Activitycompat.requestpermissions ( This, Needrequestpermissonlist.toarray (NewString[needrequestpermissonlist.size ()]), Permisson_requestcode); }    }/** * Get a list of permissions required for permission set * * @param permissions * @return * @since 2.5.0 * The Checkselfpermission method is used to determine if the app has acquired a permission * Shouldshowrequestpermissionrationale method is used to determine if * Show Request Permission dialog box, if agreed or return FALSE * * If not asked    PrivateList<string>finddeniedpermissions(string[] permissions) {List<string> needrequestpermissonlist =NewArraylist<string> (); for(String perm:permissions) {if(Contextcompat.checkselfpermission ( This, perm)! = packagemanager.permission_granted) {needrequestpermissonlist.add (perm); }Else{if(Activitycompat.shouldshowrequestpermissionrationale ( This, perm)) {Needrequestpermissonlist.add (perm); }             }        }returnNeedrequestpermissonlist; }/** * Detect if all permissions are authorized * @param grantresults * @return * @since 2.5.0 * */    Private Boolean verifypermissions(int[] grantresults) { for(intResult:grantresults) {if(Result! = packagemanager.permission_granted) {return false; }        }return true; }/** * Callback method for requesting permission results * /    @Override     Public void Onrequestpermissionsresult(intRequestcode, string[] permissions,int[] paramarrayofint) {if(Requestcode = = Permisson_requestcode) {if(!verifypermissions (Paramarrayofint))                {Showmissingpermissiondialog (); Isneedcheck =false; }        }    }/** * Show Message * * @since 2.5.0 * */    Private void Showmissingpermissiondialog() {Alertdialog.builder Builder =NewAlertdialog.builder ( This); Builder.settitle ("Hint"); Builder.setmessage ("The current app lacks the necessary permissions." Please click \ "Settings \"-\ "Permissions \"-To open the required permissions. ");//Reject, exit appBuilder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override                     Public void OnClick(Dialoginterface Dialog,intwhich) {finish ();        }                }); Builder.setpositivebutton ("Settings",NewDialoginterface.onclicklistener () {@Override                     Public void OnClick(Dialoginterface Dialog,intwhich) {startappsettings ();        }                }); Builder.setcancelable (false);    Builder.show (); }/** * Start App settings * * @since 2.5.0 * */    Private void startappsettings() {Intent Intent =NewIntent (settings.action_application_details_settings); Intent.setdata (Uri.parse ("Package:"+ Getpackagename ()));    StartActivity (Intent); }@Override     Public Boolean OnKeyDown(intKeyCode, KeyEvent event) {if(keycode = = Keyevent.keycode_back) { This. Finish ();return true; }return Super. OnKeyDown (KeyCode, event); }}

I have added some comments in the class above, and you can see it by looking closely.

Of course, not only the above one implementation method, GitHub has a lot of big God open source packaging library, can be very convenient to achieve the right to adapt. I recommend two libraries, you choose according to the needs of:

1. Permissionsdispatcher

2. Mpermissions of the Great God of Hon Yang

5. Reference

1. Android M New Run-time permissions developers need to know everything

2. Gold Map Positioning API

Talk about dynamic rights management for Android 6.0

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.