Customize your own dynamic application authority Library

Posted by run2web on Sun, 29 Mar 2020 17:54:24 +0200

Why to customize your own dynamic application authority Library

After Android 6.0, Android system requires dynamic application for sensitive permissions. This user brings great convenience, but also brings greater challenges to developers. The official API is provided for developers to use, but it's a bit cumbersome to use. There are also many open-source permission application libraries in the market. Before, rxPermission was used. This time, there are reference codes for rxPermission. It's very convenient to use the open source library, but I think the significance of open source is to provide a way of thinking and a reference for other peers. You'd better be able to write your own library, even if it doesn't matter at all.

Thinking of dynamic application authority

  • First, the official way is to apply for permission in the current Activity and call back the result in the current Activity. This kind of writing is very standard, but it is very tedious and inconvenient to write.
  • Second, I saw a library written by a colleague. He jumped to a new Activity, applied for permission, and listened to the callback 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.
  • Third, add a Fragment to the current Activity, apply for permission in the Fragment, and listen for the returned results in the Fragment. This is the idea of rxPermission. I think this is the best of the three methods, and the final version also adopts this method.

Code implementation steps

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

Use example

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



The writing is relatively simple. Please give me more advice.

Project address

Topics: Android Fragment github