Android6.0 permission management tool EasyPermissionUtil
Preface
At the beginning of android6.0, the permission application has changed, and the application has changed to dynamic, that is, the runtime permission. Similar to iOS, dynamic means that, each time you use a method that requires dangerous permissions, you need to check whether the program has obtained the permission. The dynamic permission application gives users a clearer picture of the permissions required by the program and the operations in the program that involve user security. It is no longer just to list all the common and dangerous permissions required at one time during program installation, and then display them to users.
When the target Sdk of the Project is 23, crash occurs when running the code that requires permissions because the user does not dynamically check permissions, when your target Sdk is less than 23, you do not need to force dynamic monitoring of permissions. At this time, the app will not crash and can still use functions normally.
The methods for dynamically applying for permissions on the Google official website are divided into Activity and Fragment. You can apply for permissions and check permissions through ContextCompat and sub-classes, ActivityCompat and FragmentCompat, the application method is to pop up a dialog box that cannot be rewritten by the system. The result is returned through the onRequestPermissionResult () method of Activity and Fragment.
For details, refer to the official website.
However, this kind of code will encourage the previous project to make a big transformation or write the code logic in the same method, which is inconvenient and bloated. Therefore, the following EasyPermissionUtil method simplifies permission requests and makes the code logic clearer.
EasyPermissionUtil: simplify permission requests
Because the permission requests and returned results need to be operated separately by Activity and Fragment, this will be troublesome, so it is opportunistic in EasyPermissionUtil, by enabling a new activity for permission application and check, you do not need to distinguish between multiple situations. At the same time, you can also unify all application processes and results with EasyPermissionUtil for processing.
Next, let's take a look at the overall idea:
Usage
PermissionUtil. getInstance (). request (MainActivity. this, new String [] {Manifest. permission. READ_CALENDAR}, mRequestCode, new PermissionResultCallBack () {@ Override public void onPermissionGranted () {// After the application for all permissions is approved by the user, this method will be called} @ Override public void onPermissionDenied (String... permissions) {// when one or more permissions in the permission application are denied by the user and no reminder is sent, that is, the permission application window cannot pop up, this method will be called} @ Override public void onRationalShow (String... permissions) {// when one or more permissions in the permission application are denied by the user but are not confirmed, however, if it is denied, the method will be called .}});
For download and introduction of the project source code, see github.
Project source code
In PermissionUtil, you must:
1. Check permissions
2. Apply for permissions without permission
3. Return the permission Application Result
Public class PermissionUtil {private PermissionResultCallBack mPermissionResultCallBack; private volatile static PermissionUtil instance; private int mRequestCode; private Context mContext; private Fragment mFragment; private List
MPermissionListNeedReq; private String [] mPermissions; public static PermissionUtil getInstance () {if (instance = null) {synchronized (PermissionUtil. class) {if (instance = null) {instance = new PermissionUtil () ;}} return instance ;} /*** request permission for fragment * @ param permissions * @ param requestCode * @ param callBack */public void request (@ NonNull Fragment fragment, @ NonNul L String [] permissions, @ NonNull int requestCode, PermissionResultCallBack callBack) {this. mFragment = fragment; this. request (fragment. getActivity (), permissions, requestCode, callBack );} /*** request permission in activity * @ param context * @ param permissions * @ param requestCode * @ param callBack */public void request (@ NonNull Context context, @ NonNull String [] permissions, @ NonNull int requestCode, Permissi OnResultCallBack callBack) {if (Looper. myLooper ()! = Low. getMainLooper () {throw new RuntimeException ("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 = new ArrayList
(); If (needToRequest () {requestPermissions () ;}else {onGranted ();}} /*** enable a new activity as the medium for permission application */private void requestPermissions () {Intent intent = new Intent (mContext, HelpActivity. class); intent. putExtra ("permissions", mPermissions); intent. putExtra ("requestCode", mRequestCode); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); mContext. startActivity (intent);}/*** check whether permission application is required * @ ret Urn */private boolean needToRequest () {for (String permission: mPermissions) {int checkRes = ContextCompat. checkSelfPermission (mContext, permission); if (checkRes! = PackageManager. PERMISSION_GRANTED) {PermissionInfo = new PermissionInfo (permission); if (mContext instanceof Activity & ActivityCompat. shouldShowRequestPermissionRationale (Activity) mContext, permission) {info. setRationalNeed (true);} mPermissionListNeedReq. add (info) ;}} if (mPermissionListNeedReq. size ()> 0) {mPermissions = new String [mPermissionListNeedReq. size ()]; for (int I = 0; I <MPermissionListNeedReq. size (); I ++) {mPermissions [I] = mPermissionListNeedReq. get (I ). getName ();} return true;} return false;}/*** returns * @ param requestCode * @ param permissions * @ param grantResults */@ TargetApi (Build. VERSION_CODES.M) protected void onRequestPermissionResult (int requestCode, String [] permissions, int [] grantResults) {if (requestCode = mRequestCode) {if (mContext! = Null & mContext instanceof Activity) {(Activity) mContext). onRequestPermissionsResult (requestCode, permissions, grantResults);} if (mFragment! = Null) {mFragment. onRequestPermissionsResult (requestCode, permissions, grantResults);} boolean isAllGranted = true; List
NeedRationalPermissionList = new ArrayList
(); List
DeniedPermissionList = new ArrayList
(); For (int I = 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 the permission is authorized by the user */private void onGranted () {if (mPermissionResultCallBack! = Null) {mPermissionResultCallBack. onPermissionGranted () ;}/ *** callback method after the permission application is denied by the user. This is mainly because when the user clicks the deny button and does not pop up, * When you apply for permissions again, this method will be called * @ param list */private void onDenied (List
List) {if (list = null | list. size () = 0) return; String [] permissions = new String [list. size ()]; for (int I = 0; I <list. size (); I ++) {permissions [I] = list. get (I ). getName ();} if (mPermissionResultCallBack! = Null) {mPermissionResultCallBack. onPermissionDenied (permissions) ;}}/*** callback method after the permission application is denied by the user. The main scenario is that when the user clicks deny, but does not click it, * When you apply for permissions again, this method will be called * @ param list */private void showRational (List
List) {if (list = null | list. size () = 0) return; String [] permissions = new String [list. size ()]; for (int I = 0; I <list. size (); I ++) {permissions [I] = list. get (I ). getName ();} if (mPermissionResultCallBack! = Null) {mPermissionResultCallBack. onRationalShow (permissions );}}}
In PermissionResutCallBack, do the following:
1. Return the corresponding results
Public interface PermissionResultCallBack {/*** when the application for all permissions is approved by the user, this method will be called */void onPermissionGranted (); /*** when one or more permissions in the permission application are denied by the user and no reminder is sent, that is, the permission application window cannot pop up, * This method will be called * @ param permissions */void onPermissionDenied (String... permissions);/*** when one or more permissions in the permission application are denied by the user but are not confirmed, however, if it is denied, * This method will be called. * @ param permissions */void onRationalShow (String... permissions );}
In HelpActivity, you must:
1. Apply for Permissions
2. Use onRequestPermissionUtil to return the result to PermissionUtil
Of course, this activity must be transparent without any view, so it does not seem like opening 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 application @ TargetApi (Build. VERSION_CODES.M) private void handleIntent (Intent intent) {String [] permissions = intent. getStringArrayExtra ("permissions"); int requestCode = intent. getIntExtra ("requestCode", 42); ActivityCompat. requestPermissions (this, permissions, requestCode);} @ Override protected void onDestroy () {super. onDestroy ();} // returned result @ Override public void onRequestPermissionsResult (int requestCode, String [] permissions, int [] grantResults) {PermissionUtil. getInstance (). onRequestPermissionResult (requestCode, permissions, grantResults); finish ();}}