How does Android apply for permission dynamically

  • 2021-11-24 03:02:32
  • OfStack

OverView

Today, when reviewing, I suddenly reviewed our camera operation, but for camera operation, what is more complicated for me is the operation of authority. So we need to sort out some notes on our camera operation to deepen our memory.

Development environment

Android Studio 3.6

Android 11(R)

This note uses java

Basic knowledge of permission application

Learn from: GOOGLE ANDROID DEVELOPERS

For the permissions we need to apply for, we need to do the following.

We need to add all the permissions we need to request to the App/src/main/AndroidManifest. xml file.

However, permissions are also divided into conventional permissions and dangerous permissions. For conventional permissions, we only need to add them in AndroidManifest. xml files to use them. However, for those that are regarded as dangerous permissions, we need to let users choose when to agree to apply.

Next, let's look at how to operate our dangerous permissions.

How do I determine if I have apply for permission

According to international practice, let's first look at the source code:


private boolean checkPermission() {
  //first we need check this Drive has? CAMERA Permission
  if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, CHECK_PERMISSION_CAMERA);
    return false;
  } else
    return true;
}

Here we use our camera permissions as an example.

First of all, we need to judge that our use has camera permission and can be used. After the application is completed, we need to apply for permission. We apply for the permission we need and pass it into our specified code. Here my value CHECK_PERMISSION_CAMERA is 1.

Then we need to rewrite our system method onRequestPermissionsResult

This method can be a good way to judge when the user agreed to our permission application.

First, let's look at our source code:


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  switch (requestCode) {
    case CHECK_PERMISSION_CAMERA: {
      if (grantResults.length > 0 && permissions[0].equals(PackageManager.PERMISSION_GRANTED)) {
        Toast.makeText(this, "The camera permission application wes successful!", Toast.LENGTH_SHORT).show();
        dispatchTakePictureIntent();
      } else {
        Toast.makeText(this, "The camera permission application wes failed!", Toast.LENGTH_SHORT).show();
      }
    }
    break;
  }
}

This is all the code that I used to judge when the user agreed to apply for camera permission.

This is also very simple. We judge by the identification code passed in when applying. If the code passes, we need to verify the length of grantResults and the index position in the permission array to judge whether the user agrees with our permission application. If we agree, we can proceed to the next step.

This part of the code is still very simple. The application for dynamic permission is very simple.


Related articles: