6.0权限适配的PermissionsDispatcher简单使用

动态权限之前一直使用自己封装的,还有RxPermission和EasyPermission,今天简单尝试下PermissionsDispatcher

引入

implementation "org.permissionsdispatcher:permissionsdispatcher:4.5.0"
annotationProcessor "org.permissionsdispatcher:permissionsdispatcher-processor:4.5.0"

使用的Activity上加上注解@RuntimePermissions
在这里插入图片描述
使用权限的方法上添加注解
多个权限
@NeedsPermission({Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE})
单个权限
@NeedsPermission(Manifest.permission.CAMERA)
这里使用拍照并回显,涉及到两个权限:

    @NeedsPermission({Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE})
    void showCamera() {//这个方法是由PermissionsDispatcher来调用的
        Toast.makeText(this,"获取到需要的权限了",Toast.LENGTH_SHORT).show();
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            String filename = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.CHINA).format(new Date()) + ".png";
            File file = new File(Environment.getExternalStorageDirectory(), filename);
            mCurrentPhotoPath = file.getAbsolutePath();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            startActivityForResult(takePictureIntent, 0);
        }
    }

点击Make Project 菜单
在这里插入图片描述
重写onRequestPermissionsResult方法

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        TestActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

调用:(这里调用,PermissionsDispatcher会动态申请权限,如果申请到了就会自动调用showCamera方法)

    public void takePhoto(View view) {
        TestActivityPermissionsDispatcher.showCameraWithPermissionCheck(this);
    }

完成的activity代码如下

@RuntimePermissions
public class TestActivity extends AppCompatActivity {
    private ImageView mImageView;
    private String mCurrentPhotoPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        mImageView = findViewById(R.id.iv);
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
    }

    public void takePhoto(View view) {
        TestActivityPermissionsDispatcher.showCameraWithPermissionCheck(this);
    }

    @NeedsPermission({Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE})
    void showCamera() {
        Toast.makeText(this,"获取到权限了",Toast.LENGTH_SHORT).show();
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            String filename = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.CHINA).format(new Date()) + ".png";
            File file = new File(Environment.getExternalStorageDirectory(), filename);
            mCurrentPhotoPath = file.getAbsolutePath();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            startActivityForResult(takePictureIntent, 0);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        TestActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Logger.d(mCurrentPhotoPath);
        if (resultCode == RESULT_OK && requestCode == 0) {
            Glide.with(this)
                    .load(mCurrentPhotoPath)
                    .into(mImageView);
        }
    }

}

效果图如下

补充:
如果在用户拒绝了权限时需要给用户提示,需要保证两个方法的权限是一致的(个数和内容都相等),否则OnPermissionDenied对应的方法不会执行;

    @NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void getFile() {
        //do something
    }

	//决绝了一个或者多个权限
    @OnPermissionDenied({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void showRecordDenied() {
        ToastUtils.showShort("拒绝将会影响到功能使用");
    }

仅此记录,简单的动态申请拍照和访问外部存储权限,用来拍照并回显;
其他详细使用请参考:
PermissionsDispatcher

你可能感兴趣的:(android)