Objective
android6.0, the request for permission has changed, the application is dynamic, that is, the implementation of the permissions, and iOS, the dynamic means that each time the use of the method requires critical permissions. You need to check to see if the program is licensed for that permission. Dynamic permission requests allow users to know more clearly what permissions a program needs. And where operations in the program need to involve user security.
is no longer only in the program installation time, once the need for the normal, critical level of permissions listed once. Then show it to the user.
When the target SDK for project project is 23 o'clock, crash occurs when executing to code that requires permission, given that the user assumes that there is no dynamic check for permissions. When your target SDK is below 23, the dynamic monitoring of permissions is not enforced, and the app does not crash at this time. It is still possible to use the function normally.
Google official on-line dynamic application of the method of access to the activity and fragment the two, through the Contextcompat and sub-class, Activitycompat and Fragmentcompat to the permission of the application and permission to check, The way to apply is to pop up a system that does not overwrite the dialog box. The result is a return through activity and the Onrequestpermissionresult () method of the fragment.
Detailed to be able to participate in the Examiner network
However, this code can cause the previous project to be transformed or the logic of the code will be coupled to the same method. Obviously inconvenient and bloated.
So the following easypermissionutil is the way to simplify permission requests at the same time to make the logic of the code clearer.
Easypermissionutil: Simplifying permission requests
Because the request for permission and the return of the result need to separate activity and fragment two kinds of operation, this will be more troublesome, so easypermissionutil in the opportunistic, by opening a new activity for permission to apply and check the operation. This does not need to distinguish between different situations, at the same time can be all the application process and the results of the unified easypermissionutil to deal with.
Next look at the overall idea:
The method used
Permissionutil.getinstance (). Request (mainactivity. This,NewString[]{manifest.permission.read_calendar}, Mrequestcode,NewPermissionresultcallback () {@Override Public void onpermissiongranted() {The method is called when the full permission request is allowed by the user.}@Override Public void onpermissiondenied(String ... permissions) {When one or more of the permissions in the permission request is denied by the user and confirms that no more reminders are given, the request form for the permission cannot be ejected again, and the method will be called}@Override Public void Onrationalshow(String ... permissions) {//When one or more of the permissions in a permission request is denied by the user, but there is no confirmation no longer reminded, that is, when the permission form is requested, but is denied, the method will be called.} });
Project source code download and introduction. Take a look at GitHub.
Project Source Code
In Permissionutil, the thing to do is:
1. Check for permissions
2. Permission to apply for authorization without permission
3. Returns the result of a permission request
Public class permissionutil { PrivatePermissionresultcallback Mpermissionresultcallback;Private volatile StaticPermissionutil instance;Private intMrequestcode;PrivateContext Mcontext;PrivateFragment mfragment;PrivateList<permissioninfo> Mpermissionlistneedreq;PrivateString[] mpermissions; Public StaticPermissionutilgetinstance() {if(Instance = =NULL) {synchronized(Permissionutil.class) {if(Instance = =NULL) {instance =NewPermissionutil (); } } }returnInstance }/** * For request permission in fragment * @param fragment * @param permissions * @param Reque Stcode * @param callBack * * Public void Request(@NonNull Fragment Fragment, @NonNull string[] permissions, @NonNullintRequestcode, Permissionresultcallback callBack) { This. mfragment = fragment; This. Request (Fragment.getactivity (), permissions, Requestcode, CallBack); }/** * For request permission in activity * @param context * @param permissions * @param reques Tcode * @param callBack * * Public void Request(@NonNull context context, @NonNull string[] permissions, @NonNullintRequestcode, Permissionresultcallback callBack) {if(Looper.mylooper ()! = Looper.getmainlooper ()) {Throw NewRuntimeException ("Request permission only can run in mainthread!"); }if(Permissions.length = =0) {return; }if(Build.VERSION.SDK_INT < Build.version_codes. M) {ongranted ();return; } This. Mcontext = Context; This. mpermissions = Permissions; This. Mrequestcode = Requestcode; This. Mpermissionresultcallback = CallBack; This. Mpermissionlistneedreq =NewArraylist<permissioninfo> ();if(Needtorequest ()) {requestpermissions (); }Else{ongranted (); } }/** * By opening a new activity as the medium for requesting permission * * Private void requestpermissions() {Intent Intent =NewIntent (Mcontext, Helpactivity.class); Intent.putextra ("Permissions", mpermissions); Intent.putextra ("Requestcode", Mrequestcode); Intent.addflags (Intent.flag_activity_new_task); Mcontext.startactivity (Intent); }/** * Check if you need permission to apply * @return * * Private Boolean needtorequest() { for(String permission:mpermissions) {intCheckres = Contextcompat.checkselfpermission (mcontext, permission);if(Checkres! = packagemanager.permission_granted) {Permissioninfo info =NewPermissioninfo (permission);if(McontextinstanceofActivity && Activitycompat.shouldshowrequestpermissionrationale (activity) Mcontext, permis sion)) {Info.setrationalneed (true); } mpermissionlistneedreq.add (info); } }if(Mpermissionlistneedreq.size () >0) {mpermissions =NewString[mpermissionlistneedreq.size ()]; for(inti =0; I < mpermissionlistneedreq.size (); i++) {Mpermissions[i] = Mpermissionlistneedreq.get (i). GetName (); }return true; }return false; }/** * Request permission Results return * @param requestcode * @param permissions * @param Grantresu LTS * / @TargetApi(Build.version_codes. Mprotected void Onrequestpermissionresult(intRequestcode, string[] permissions,int[] grantresults) {if(Requestcode = = Mrequestcode) {if(Mcontext! =NULL&& McontextinstanceofActivity) {(activity) mcontext). Onrequestpermissionsresult (Requestcode, permissions, grantresults); }if(Mfragment! =NULL) {Mfragment.onrequestpermissionsresult (Requestcode, permissions, grantresults); }Booleanisallgranted =true; List<permissioninfo> needrationalpermissionlist =NewArraylist<permissioninfo> (); List<permissioninfo> deniedpermissionlist =NewArraylist<permissioninfo> (); for(inti =0; i < permissions.length; i++) {if(Grantresults[i] = = packagemanager.permission_denied) {if(Mpermissionlistneedreq.get (i). Isrationalneed ()) {Needrationalpermissionlist.add (Mpermissionlistneedreq.get (i)); }Else{Deniedpermissionlist.add (Mpermissionlistneedreq.get (i)); } isallgranted =false; } }if(Needrationalpermissionlist.size ()! =0) {showrational (needrationalpermissionlist); }if(Deniedpermissionlist.size ()! =0) {ondenied (deniedpermissionlist); }if(isallgranted) {ongranted (); } } }/** * Method of callback after permission is granted by the user * / Private void ongranted() {if(Mpermissionresultcallback! =NULL) {mpermissionresultcallback.onpermissiongranted (); } }/** * Permission application is rejected by the user after the callback method, this is mainly when the user clicked the negative at the same time the click does not pop up, * Then when the permission is applied again, this method will be called * @param list */ Private void ondenied(list<permissioninfo> List) {if(List = =NULL|| List.size () = =0)return; string[] Permissions =NewString[list.size ()]; for(inti =0; I < list.size (); i++) {Permissions[i] = List.get (i). GetName (); }if(Mpermissionresultcallback! =NULL) {mpermissionresultcallback.onpermissiondenied (permissions); } }/** * Permission application is rejected by the user after the callback method, the main scenario is when the user clicked the negative, but did not click on the popup, * then when the request permission again, this method will be called * @param list */ Private void showrational(list<permissioninfo> List) {if(List = =NULL|| List.size () = =0)return; string[] Permissions =NewString[list.size ()]; for(inti =0; I < list.size (); i++) {Permissions[i] = List.get (i). GetName (); }if(Mpermissionresultcallback! =NULL) {mpermissionresultcallback.onrationalshow (permissions); } }}
In Permissionresutcallback, the thing to do is:
1. Return the corresponding result
public interface permissionresultcallback { /** * The method will be called after the application of the full permission is allowed by the user */ void onpermissiongranted (); /** * When a permission request in one or more of the permissions, by the user previously denied, and confirmed that no longer reminded, that is, the permissions of the application form can not be ejected, * The method will be called * @param permissions */ void onpermissiondenied (St Ring ... permissions); /** * When one or more permissions in the permission request is denied by the user, but there is no confirmation no longer reminded, that is, when the permission form is applied, but is denied, * The method will be called. * @param permissions */ void onration Alshow (String ... permissions);}
In the helpactivity. The thing to do is:
1. Permission to apply
2. Return results to Permissionutil by Onrequestpermissionutil
Of course, this activity must be transparent, and there is no view of whatever. It doesn't look like it's a new activity.
Public class helpactivity extends Activity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);if(Savedinstancestate = =NULL) {handleintent (getintent ()); } }@Override protected void onnewintent(Intent Intent) {handleintent (intent); }//Permission Request @TargetApi(Build.version_codes. MPrivate void handleintent(Intent Intent) {string[] permissions = Intent.getstringarrayextra ("Permissions");intRequestcode = Intent.getintextra ("Requestcode", the); Activitycompat.requestpermissions ( This, permissions, Requestcode); }@Override protected void OnDestroy() {Super. OnDestroy (); }//Return results @Override Public void Onrequestpermissionsresult(intRequestcode, string[] permissions,int[] grantresults) {permissionutil.getinstance (). Onrequestpermissionresult (Requestcode, permissions, grantresults); Finish (); }}
Project source code download and introduction. Take a look at GitHub.
android6.0 Rights Management Tool Easypermissionutil