Android 6.0-Solutions for dynamic rights Management

Source: Internet
Author: User

Reprint please mark

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! Here is a very good solution to provide the source code, the project can be used directly.


Permissions

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.

The GitHub example in this article

In API 23, permissions need to be dynamically fetched, and core permissions must be met. Standard process:


Flow chart

If the user clicks and no longer prompts , the system authorization pop-up window will not eject. The process changes to:


Flow chart

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. */PublicClassPermissionschecker {PrivateFinal Context Mcontext;PublicPermissionschecker(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 the permissions are missing private boolean lackspermission (String permission) { Span class= "Hljs-keyword" >return contextcompat.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.

PublicClassMainactivityExtendsappcompatactivity {PrivateStaticFinalint Request_code =0;Request CodeAll of the required permissionsStaticFinal string[] PERMISSIONS =New string[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.MODIFY_AUDIO_SETTINGS};@Bind (r.id.main_t_toolbar) toolbar Mttoolbar;Private Permissionschecker Mpermissionschecker;Permission Detectors@OverrideProtectedvoidOnCreate(Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Butterknife.bind (this); Setsupportactionbar (Mttoolbar); Mpermissionschecker =New Permissionschecker (this); }@OverrideProtectedvoidOnresume() {Super.onresume ();When permissions are missing, enter the permissions configuration pageif (Mpermissionschecker.lackspermissions (PERMISSIONS)) {startpermissionsactivity ();}}private void startpermissionsactivity () { Permissionsactivity.startactivityforresult (this, Request_code, PERMISSIONS);}  @Override protected void onactivityresult ( int Requestcode, int ResultCode, Intent data) {super.onactivityresult (Requestcode, ResultCode, data); //rejected, close the page, missing primary permissions, unable to run if (Requestcode = Request_code && Amp 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.

/** * Permission to get page * <p/> * Created by Wangchenlong on 16/1/26. */PublicClassPermissionsactivityExtendsappcompatactivity {PublicStaticFinalint permissions_granted =0;Permission authorizationPublicStaticFinalint permissions_denied =1;Permission deniedPrivateStaticFinalint Permission_request_code =0;Parameters of the System Rights Management pagePrivateStaticFinal String extra_permissions ="Me.chunyu.clwang.permission.extra_permission";Permission parametersPrivateStaticFinal String Package_url_scheme ="Package:";SchemePrivate Permissionschecker Mchecker;Permission DetectorsPrivateBoolean Isrequirecheck;Whether system permission detection is requiredLaunches the public interface for the current permissions pagePublicStaticvoidStartactivityforresult(Activity activity,int Requestcode, String ... permissions) {Intent Intent =New Intent (activity, permissionsactivity.class); Intent.putextra (Extra_permissions, PERMISSIONS); Activitycompat.startactivityforresult (activity, intent, Requestcode,NULL); }@OverrideProtectedvoidOnCreate(@Nullable Bundle savedinstancestate) {Super.oncreate (savedinstancestate);if (getintent () = =null | | !getintent (). Hasextra (extra_permissions)) {ThrowNew RuntimeException ("Permissionsactivity needs to start with a static Startactivityforresult Method!"); } setcontentview (R.layout.activity_permissions); Mchecker =New Permissionschecker (this); Isrequirecheck =True }@OverrideProtectedvoidOnresume() {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 parameterPrivate string[] GetPermissions () {Return Getintent (). Getstringarrayextra (extra_permissions); }Request permission compatible with low versionPrivatevoidRequestpermissions(String ... permissions) {Activitycompat.requestpermissions (This, permissions, Permission_request_code); }All permissions have been obtainedPrivatevoidallpermissionsgranted() {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 */@OverridePublicvoidOnrequestpermissionsresult(int Requestcode, @NonNull string[] permissions, @NonNullInt[] grantresults) {if (Requestcode = = Permission_request_code && hasallpermissionsgranted (grantresults)) {Isrequirecheck =True Allpermissionsgranted (); }else {Isrequirecheck =False Showmissingpermissiondialog (); } }All rights included.PrivateBooleanhasallpermissionsgranted(@NonNullInt[] grantresults) {for (int grantresult:grantresults) {if (Grantresult = = packagemanager.permission_denied) {ReturnFalse } }ReturnTrue }Show missing permission promptPrivatevoidShowmissingpermissiondialog() {Alertdialog.builder Builder =New Alertdialog.builder (permissionsactivity.this); Builder.settitle (R.STRING.HELP); Builder.setmessage (R.string.string_help_text);Reject, Exit application Builder.setnegativebutton (R.string.quit,New Dialoginterface.onclicklistener () {@OverridePublicvoidOnClick(Dialoginterface Dialog,int which) {setresult (permissions_denied); Finish ();}}); Builder.setpositivebutton (R.string.settings,new Dialoginterface.onclicklistener () { @Override public void onclick (dialoginterface dialog,  int which) {startappsettings ();}}); Builder.show (); } //launch app settings private void startappsettings () {Intent Intent = new Intent (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


Custom Authorization

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.

List of dangerous permissions

Android 6.0-Solution for Dynamic Rights Management (RPM)

Related Article

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.