Welcome to follow my GitHub and follow my csdn.
The Android 6.0 version (Api 23) introduces a number of new features that greatly enhance the user experience and create a new burden for programmers. Dynamic Rights Management is the way to make it easier for users to control their privacy, on the one hand, the need to re-adapt the application permissions. Times are always evolving, procedures are always people-oriented, let us add dynamic rights Management for the application it! This provides a very good solution.
The Android system contains the default authorization prompt , but we still need to set up our own page. The reason is that the system provides the authorization box, there will be no longer prompt options. If the user chooses, the authorization prompt cannot be triggered. Using the custom prompt page, you can give the user manual instructions to modify the authorization.
This article is an example of GitHub.
In API 23, permissions need to be dynamically fetched, and core permissions must be met. Standard process:
If the user clicks and no longer prompts , the system authorization pop-up window will not eject. The process changes to:
That's the process, let's take a look at the code.
1. Permissions
In Androidmanifest, add two permissions, record and modify the volume .
<!--危险权限--> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <!--一般权限--> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
Dangerous permissions must be authorized, and general permissions are not required.
Detection Permission Class
/** * Check Permissions for the tool class * <p/> * Created by Wangchenlong on 16/1/26. * * Public class permissionschecker { Private FinalContext Mcontext; Public Permissionschecker(Context context) {mcontext = Context.getapplicationcontext (); }//Judgment permission set Public Boolean lackspermissions(String ... permissions) { for(String permission:permissions) {if(lackspermission (permission)) {return true; } }return false; }//Determine if permissions are missing Private Boolean lackspermission(String permission) {returnContextcompat.checkselfpermission (mcontext, permission) = = Packagemanager.permission_denied; }}
2. Home
Assume that the first page needs to use permissions, before the pages are displayed, that is, onresume, the detection of permissions,
If missing, then access the access page; When the return value is received and the permission is denied, it is closed directly.
Public class mainactivity extends appcompatactivity { Private Static Final intRequest_code =0;//Request Code //All permissions required Static Finalstring[] PERMISSIONS =Newstring[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.MODIFY_AUDIO_SETTINGS};@Bind(R.id.main_t_toolbar) Toolbar Mttoolbar;PrivatePermissionschecker Mpermissionschecker;//Permission Detectors @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Butterknife.bind ( This); Setsupportactionbar (Mttoolbar); Mpermissionschecker =NewPermissionschecker ( This); }@Override protected void Onresume() {Super. Onresume ();//When permissions are missing, enter the permissions configuration page if(Mpermissionschecker.lackspermissions (PERMISSIONS)) {startpermissionsactivity (); } }Private void startpermissionsactivity() {Permissionsactivity.startactivityforresult ( This, Request_code, PERMISSIONS); }@Override protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {Super. Onactivityresult (Requestcode, ResultCode, data);//Reject, close page, missing primary permission, cannot run if(Requestcode = = Request_code && ResultCode = = permissionsactivity.permissions_denied) {Finish (); } }}
Core permissions must be met, such as camera application, camera permissions are required, if the user is not authorized, then directly shut down.
3. Authorization page
Authorization page, first use the System default authorization page, when the user rejects, instruct the user to manually set, when the user fails again, return to continue prompting. When a user exits the authorization page manually, a notification is sent to use the page to send an authorization failure.
/** * Access page * <p/> * Created by Wangchenlong on 16/1/26. * * Public class permissionsactivity extends appcompatactivity { Public Static Final intpermissions_granted =0;//Permission Authorization Public Static Final intPermissions_denied =1;//Permission denied Private Static Final intPermission_request_code =0;//parameters of the System Rights Management page Private Static FinalString extra_permissions ="Me.chunyu.clwang.permission.extra_permission";//Permissions parameter Private Static FinalString Package_url_scheme ="Package:";//Programme PrivatePermissionschecker Mchecker;//Permission Detectors Private BooleanIsrequirecheck;//Whether system permission detection is required //Start the public interface of the current permissions page Public Static void Startactivityforresult(Activity activity,intRequestcode, String ... permissions) {Intent Intent =NewIntent (activity, permissionsactivity.class); Intent.putextra (Extra_permissions, PERMISSIONS); Activitycompat.startactivityforresult (activity, intent, Requestcode,NULL); }@Override protected void onCreate(@Nullable Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);if(getintent () = =NULL|| !getintent (). Hasextra (extra_permissions)) {Throw NewRuntimeException ("Permissionsactivity needs to start with a static Startactivityforresult method!"); } setcontentview (R.layout.activity_permissions); Mchecker =NewPermissionschecker ( This); Isrequirecheck =true; }@Override protected void Onresume() {Super. Onresume ();if(Isrequirecheck) {string[] permissions = GetPermissions ();if(mchecker.lackspermissions (permissions)) {requestpermissions (permissions);//Request Permission}Else{allpermissionsgranted ();//All permissions have been obtained} }Else{Isrequirecheck =true; } }//Returns the passed permission parameter PrivateString[]getPermissions() {returnGetintent (). Getstringarrayextra (extra_permissions); }//Request permission compatible with low version Private void requestpermissions(String ... permissions) {Activitycompat.requestpermissions ( This, permissions, Permission_request_code); }//All permissions have been obtained Private void allpermissionsgranted() {Setresult (permissions_granted); Finish (); }/** * User Rights processing, * if all gets, then directly. * If the permissions are missing, prompt dialog. * * @param requestcode Request code * @param Permissions permissions * @param grantresults Results */ @Override Public void Onrequestpermissionsresult(intRequestcode, @NonNull string[] permissions, @NonNullint[] grantresults) {if(Requestcode = = Permission_request_code && hasallpermissionsgranted (grantresults)) {Isrequirecheck =true; Allpermissionsgranted (); }Else{Isrequirecheck =false; Showmissingpermissiondialog (); } }//With full permissions Private Boolean hasallpermissionsgranted(@NonNullint[] grantresults) { for(intGrantresult:grantresults) {if(Grantresult = = packagemanager.permission_denied) {return false; } }return true; }//Show missing permission prompt Private void Showmissingpermissiondialog() {Alertdialog.builder Builder =NewAlertdialog.builder (permissionsactivity. This); Builder.settitle (R.STRING.HELP); Builder.setmessage (R.string.string_help_text);//Reject, exit appBuilder.setnegativebutton (R.string.quit,NewDialoginterface.onclicklistener () {@Override Public void OnClick(Dialoginterface Dialog,intwhich) {Setresult (permissions_denied); Finish (); } }); Builder.setpositivebutton (R.string.settings,NewDialoginterface.onclicklistener () {@Override Public void OnClick(Dialoginterface Dialog,intwhich) {startappsettings (); } }); Builder.show (); }//Start the app's settings Private void startappsettings() {Intent Intent =NewIntent (settings.action_application_details_settings); Intent.setdata (Uri.parse (Package_url_scheme + getpackagename ())); StartActivity (Intent); }}
Note the use of the Isrequirecheck parameter to prevent overlapping with the system hint box.
System authorization prompt: Activitycompat.requestpermissions, activitycompat compatible low version.
Effect
The key part of this is that, although the dynamic authorization to the programmer to bring some trouble, but it is necessary for the user, we should also welcome, after all, each programmer is a half product manager.
OK, that ' s all! Enjoy it.
Android 6.0: A dynamic Rights management solution