Customize your own dynamic application permission library

Why customize your own dynamic application permission library

After Android 6.0, the Android system requires dynamic application for sensitive permissions. This user brings great convenience, but also brings greater challenges to developers. The official provides the corresponding API for developers to use, but it is a little troublesome to use. There are also many open source permission application libraries in the market. I used rxPermission before, and this time I also refer to the code of rxPermission. It is very convenient to use open source libraries, but I think the significance of open source is to provide a way of thinking and give other colleagues a reference. It is best for everyone to be able to write their own libraries, even if they rub a little.

The idea of ​​dynamically applying for permissions

  • The first one: The official way of writing is to apply for permission in the current Activity and call back the result in the current Activity. This way of writing is very standard, but it is very cumbersome and very inconvenient to write.
  • The second: I saw a library written by a colleague before. He jumped to a new Activity, then applied for permission, and listened for callbacks in the Activity. This method achieves the effect of convenient use, but the experience is not very good. The first version I wrote was this method.
  • The third type: Add a Fragment to the current Activity, apply for permission in the Fragment, and monitor the returned result in the Fragment. This is the idea of ​​rxPermission. I think this is the best method among the three methods, and this method is also adopted in the final version.

Code implementation steps

  • First, write an entry class: SchPermission, the constructor passes in the activity, and creates a fragment to add to the activity.
  • Then, create a Fragment class: SchPermissionFragment. The specific application permission application and application result callback are all in this fragment.
  • In order to prevent calling the application permission interface multiple times at the same time, resulting in confusion of callbacks, the requestCode of each application for permission is different, create a HashMap to store requestCode and callback, one-to-one correspondence, to prevent callback confusion.

Example of use

  SchPermission mSchPermission = new SchPermission(this);
  mSchPermission
                        .request(new String[] {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                new
                                        IPermissionCallback() {
                                            @Override
                                            public void onPermissionResult(@NonNull String[] permissions,
                                                                           @NonNull int[] grantResults,
                                                                           boolean[]
                                                                                   shouldShowRequestPermissionRationale) {
                                                StringBuffer sb = new StringBuffer();
                                                for (String str : permissions) {
                                                    sb.append(" ");
                                                    sb.append(str);
                                                }
                                                StringBuffer sb2 = new StringBuffer();
                                                for (int i : grantResults) {
                                                    sb2.append(" ");
                                                    sb2.append(i);
                                                }
                                                StringBuffer sb3 = new StringBuffer();
                                                for (boolean b : shouldShowRequestPermissionRationale) {
                                                    sb3.append(" ");
                                                    sb3.append(b);
                                                }
                                                Log.i(TAG, "permissions=" + sb.toString() + ",grantResults=" + sb2
                                                        .toString() + ",shouldShowRequestPermissionRationale=" + sb3
                                                        .toString());

                                            }
                                        });
    

postscript

It is relatively simple to write, please give me more advice.

project address

https://github.com/shench5612390/SchPermission/tree/master

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325021650&siteId=291194637