Android Ultra Clear 6.0 Permission Application AndPermission

  • 2021-11-13 18:06:04
  • OfStack

Android ultra-clear 6.0 authority application AndPermission specific implementation code, for your reference, the specific content is as follows

Preface

This is the framework I often use for the following reasons:

1. Clear thinking
2. Easy to implement

Begin

Preparatory work

Guide pack


compile 'com.yanzhenjie:permission:1.0.7'

Then you can use it. The thief is simple

Use

Step 1


findViewById(R.id.selectPic).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        // Detection authority 
        AndPermission.with(MainActivity.this)
            .requestCode(202)
            .permission(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            .callback(listener)
            .start();
      }
    });

Explanation:

1. requestCode (): Used for callback to determine where it was requested
2. permission (): Just fill in the permission to apply directly, but 1 must be added in AndroidManifest. xml
3. callback (): Just fill in the callback object

When you click the button, it will automatically query whether you have permission. If you don't, an application box will pop up. If you have permission, you will directly go back to the success method inside.

Step 2 Callback Object


// Permission listening callback 
  private PermissionListener listener = new PermissionListener() {
    @Override
    public void onSucceed(int requestCode, List<String> grantedPermissions) {
      //  The permission request was successfully called back. 
      if (requestCode == 202) {
        // Actions after successful application 
      }

    }
    @Override
    public void onFailed(int requestCode, List<String> deniedPermissions) {
      //  Permission request failed callback. Can be prompted 
     }
  };

That's it. It's really simple.


Related articles: