Android6.0 adopt the new permission model, only when the permission is required to inform the user whether authorization, is authorized at runtime, not when the original installation, and by default each time you open the page at run time, you need to check whether the required permission to apply. Such a user's autonomy improved a lot, such as the user can give the app camera permissions, but can refuse to record the location of the device permissions, is afraid of location information upload and so on.
1. A simple permission encapsulation class
First define a tool class to determine if a permission is missing:
//when checking permissions, determine the set of permissions for the system Public BooleanPermissionSet (String ... permissions) { for(String permission:permissions) {if(islackpermission (permission)) {//whether to add a complete set of permissions return true; } } return false; } //Check system permissions to determine whether the permissions are currently missing (permission_denied: Sufficient permissions) Private Booleanislackpermission (String permission) {returnContextcompat.checkselfpermission (context, permission) = =packagemanager.permission_denied; }
Create a baseactivity to do the encapsulation
//first DECLARE permission authorization Public Static Final intPermission_denieg = 1;//Insufficient permissions, when permissions are denied Public Static Final intPermission_request_code = 0;//result parameters when the system authorizes the administration of the page Public Static FinalString package_url_scheme = "package:";//Permission Scheme PublicCheckpermission checkpermission;//permission detectors for detection permission classes Private BooleanIsrequestcheck =true;//Determine if system permission detection is required. Prevent overlapping with system prompts Public Abstract intInitcontentid (); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Initcontentid (); Process (savedinstancestate); } //Show dialog box prompting user for missing permissions Public voidShowmissingpermissiondialog () {Alertdialog.builder Builder=NewAlertdialog.builder ( This); Builder.settitle (r.string.help);//Tips Helpbuilder.setmessage (R.string.string_help_text); //If you are denying authorization, exit the app//ExitBuilder.setnegativebutton (R.string.quit,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Finish (); } }); //open settings, let the user choose to open PermissionsBuilder.setpositivebutton (R.string.settings,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {startappsettings ();//Open Settings } }); Builder.setcancelable (false); Builder.show (); } //Get full Permissions Public BooleanHasallpermissiongranted (int[] grantresults) { for(intgrantresult:grantresults) { if(Grantresult = =packagemanager.permission_denied) { return false; } } return true; } //Open System App settings (action_application_details_settings: System settings Permissions) Public voidstartappsettings () {Intent Intent=NewIntent (settings.action_application_details_settings); Intent.setdata (Uri.parse (Package_url_scheme+getpackagename ())); StartActivity (Intent); } //Request permission to compatible version Public voidrequestpermissions (String ... permission) {activitycompat.requestpermissions ( This, permission, Permission_request_code); } /*** for Rights Management * if fully authorized, then directly through the * if permission is denied, the dialog hint is used when the permission is missing * *@paramRequestcode Request Code *@paramPermissions Permission Parameters *@paramgrantresults Results*/@Override Public voidOnrequestpermissionsresult (intRequestcode, string[] permissions,int[] grantresults) { Super. Onrequestpermissionsresult (Requestcode, permissions, grantresults); if(Permission_request_code = = Requestcode && hasallpermissiongranted (grantresults))//determine if the request code is consistent with the request result{Isrequestcheck=true;//need to detect permissions, go directly, otherwise prompt dialog box to setgetallgrantedpermission (); } Else { //prompt dialog box settingsIsrequestcheck =false; Showmissingpermissiondialog ();//Dialog } } /** When the required permissions are obtained, the relevant business operations*/ Public voidgetallgrantedpermission () {}protected voidprocess (Bundle savedinstancestate) {if(GetPermissions ()! =NULL) {checkpermission=NewCheckpermission ( This); if(Checkpermission.permissionset (GetPermissions ())) {requestpermissions (getPermissions ()); //to request permission}Else{getallgrantedpermission (); } }} @Overrideprotected voidOnresume () {Super. Onresume (); //based on activity life cycle, Onrestart ()->onresume ()//This means that after returning from the System Settings page, check to see if the user has the required permissions open if(!Isrequestcheck) { if(GetPermissions ()! =NULL) { if(Checkpermission.permissionset (GetPermissions ())) {Showmissingpermissiondialog ();//Dialog}Else { //get full permission to go normal businessgetallgrantedpermission (); } } } Else{Isrequestcheck=true; } } Publicstring[] GetPermissions () {return NULL; }
In your activity, rewrite the following methods
Static Finalstring[] PERMISSION =Newstring[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,//Write PermissionsManifest.permission.READ_EXTERNAL_STORAGE,//Read PermissionsManifest.permission.READ_PHONE_STATE,//Read device informationManifest.permission.ACCESS_COARSE_LOCATION,//Baidu PositioningManifest.permission.ACCESS_FINE_LOCATION,}; //The layout ID is sent to baseactivity.@Override Public intInitcontentid () {returnR.layout.activity_main; } @Overrideprotected voidprocess (Bundle savedinstancestate) {Super. Process (savedinstancestate); //If there is anything that needs to be initialized, write it here .} @Override Public voidgetallgrantedpermission () {//when the required permissions are obtained, the related business operations Super. Getallgrantedpermission (); } @Override Publicstring[] GetPermissions () {returnPERMISSION; }
Android6.0 Dynamic Get Permissions