Detailed explanation of dynamic application permission for Android 6.0 and above

Keywords: Android

Unknowingly, Android has arrived at 8.0. I remember when I first came out of 6.0, I saw some articles written by others. It's almost two years ago. I wonder how fast time has passed, but I don't know how much progress I have made. Maybe because I am lazy and not good at summarizing, I sometimes see a knowledge point before I know it, but I forget it. When I slowly write notes later, I found that note-taking is really important to myself. If a certain knowledge is forgotten, I will know that I have recorded it all at once. In a certain place, if I do not summarize it, I will spend a lot of time looking for information. So I hope that students should organize a summary of their own. Okay, let's say dynamic application for permission. There are many articles in this field on the internet, some of which are also well written. I think I can record them as my own thing, and at the same time, I can give some references to the students who have just come into contact with this area.
1. First of all, we should judge whether the android version is 23 or above.

Build.VERSION.SDK_INT >=  Build.VERSION_CODES.M

2. Dynamic application of the permissions we need, here apply for three permissions: camera photography, storage, dialing, and then an array to install:

  private String[] mPermission = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE
            , Manifest.permission.CAMERA
            , Manifest.permission.CALL_PHONE
    };

3. Here we can start to call the requestPermissions () method to apply for permission. Specific code:

  if  (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            ActivityCompat.requestPermissions(this,mPermission,mRequesCode);
        }

This can be done on these two lines, where mRequestCode pre-declared int type variables, mainly to deal with the application permission after the return of the results of processing, code:

 private final  int mRequestCode = 321;

4. The next step is to process the return result of the application permission, which is to rewrite the onRequestPermissionsResult () method. The specific code is as follows:

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //Determine whether it is a request code to apply for permission?
        if (requestCode == mRequestCode){
            //Judging whether the version is 23 or more, you can also make no judgment, because you have done 23 in advance before applying for permission dynamically?
            if (Build.VERSION.SDK_INT >=  Build.VERSION_CODES.M){
                //The other two permissions are ignored, whether the application is successful or not, and the main test here is whether the storage application is successful.
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
                    //Should Show Request Permission Rationale judge whether a user clicks on a reminder. (Check if the permission is still available)
                    //If you click on it and no longer remind us to prompt the user to go to the reference permission list to open permissions manually
                    //If we refuse, we will apply to the user again to open the storage permission.
                    boolean b = shouldShowRequestPermissionRationale(mPermission[0]);
                    if (!b){
                        //Not prompted
                        showDialogTipUserGoToAppSettting();
                    }else {
                        //refuse
                        getPermissions(mStorage,mRequestCode);
                    }
                }
            }
        }
    }

Among them, getPermissions(mStorage,mRequestCode) is a method that I encapsulate beforehand to facilitate multiple calls. Actually, it is written as ActivityCompat. Request Permissions (this, mPermission, mRequesCode); the permissions to be applied and the request codes are passed in, which are written at the beginning, and will not be repeated.

5. Next, the user is prompted to apply the permission list to set permissions dynamically. That is the showDialogTipUserGoToAppSetting () method above. The code is as follows:

 private void showDialogTipUserGoToAppSettting() {
       AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton("Determine", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivityForResult(intent, 123);
            }
        });
        builder.setTitle("Storage permissions are unavailable");
        builder.setMessage("Please manually open storage permissions in application permission settings");
        builder.show();
    }

The above code should be well understood is to open the list of corresponding application permissions through Intent intent, and then rewrite the onActivityResult method to handle whether the storage permissions are successfully opened manually. The specific code is as follows:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123) {
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // Check if this permission has been obtained
                int i = ActivityCompat.checkSelfPermission(this, mPermission[0]);
                // Has permission been granted GRANTED - - authorized DINIED - - refused
                if (i != PackageManager.PERMISSION_GRANTED) {
                    // Indicates that users should go to the application settings interface to manually open permissions
                    showDialogTipUserGoToAppSettting();
                }
            }
        }
    }

Finally, we need to add the above permissions statically in Manifest

 <uses-permission android:name="android.permission.CALL_PHONE" />
  <uses-permission android:name="android.permission.CAMERA" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

The above code has been annotated in detail. The main part of code for dynamic permission application has been posted in detail. We hope we can understand more about the shortcomings. Thank you.

Posted by R_P on Tue, 11 Jun 2019 11:02:59 -0700