封装android运行时权限(EPermission)

介绍自己写的一个关于android6.0运行时权限封装库

在我的上一篇文章详解android6-0权限 中解释了关于android 6.0后关于运行时权限的判断,申请,回调,我们采用的是常规的申请流程,这篇文章我们将要根据申请的流程,基于Annotation Processing Tool技术,封装一个EPermission的库,方便大家使用

使用

下面介绍一下EPermission的使用

使用到注解

  • @PermissionGrant 标识某个权限被同意授权后,回调的方法
  • @PermissionDeny 标识某个权限被拒绝授权后,回调的方法
  • @PermissionRationale 标识某个权限初次被拒绝后,下次再申请,如果需要对用户显示权限解释,回调的方法

基本原理
根据请求权限时的requestCode和requestPermission以及处理结果grantResult,寻找对应的注解方法进行回调

引入

dependencies {
    annotationProcessor 'com.eggsy:epermission-processor:1.1.1'
    compile 'com.eggsy:epermission:1.1.1'
    compile 'com.eggsy:epermission-annotations:1.1.1'
}

权限判断

判断你是否已经有某个权限

ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

判断是否需要调用权限解释,true代表需要显示,会回调具有对应REQUEST_SINGLE_PERMISSON和Manifest.permission.READ_EXTERNAL_STORAGE)的@PermissionRationale标识的方法

EPermission.shouldShowRequestPermissionRationale(context, REQUEST_SINGLE_PERMISSON, Manifest.permission.READ_EXTERNAL_STORAGE)

申请单个权限,如果已经授权,直接回调,@PermissionGrant注解方法,如果用户已经永久拒绝授予该权限,直接回调@PermissionDeny注解方法,如果都不是以上情况最后会弹出权限申请框提示用户授权

EPermission.requestPermissions(context, REQUEST_SINGLE_PERMISSON, Manifest.permission.READ_EXTERNAL_STORAGE);

一次性申请多个权限,如果多个权限中某些已经授权,会依次回调@PermissionGrant注解方法,如果永久拒绝,会依次调用@PermissionDeny注解方法,其余的会依次弹出提示框申请权限,然后在所有结果都处理完之后,回调对应的注解方法

EPermission.requestPermissions(context, REQUEST_MULTI_PERMISSON, Manifest.permission.CAMERA, Manifest.permission.READ_CONTACTS);

回调权限处理结果

回调用户的权限处理结果

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

注意:
这里主要原理是依次根据requestCode,permission和对应的grantResult寻找对应的注解方法,回调方法的确定规则:

  1. 根据requestCode和permission确定回调哪些符合条件的注解方法,这里要注意如果注解没有标识requestCode属性,那么该注解方法可以接收其注解对应permission的所有回调
  2. 每个permission处理结果grantResult用来确认是调用@PermissionGrant还是@PermissionDeny对应的注解方法

举两个栗子

在Activity中使用

public class PermissionActivity extends Activity implements View.OnClickListener {

    private static final String TAG = "permission";

    private static final int REQUEST_SINGLE_PERMISSON = 1;
    private static final int REQUEST_MULTI_PERMISSON = 2;

    Button mBtnRequestSinglePermission;
    Button mBtnRequestMultiPermission;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);

        initView();
    }

    private void initView() {
        mBtnRequestSinglePermission = (Button) findViewById(R.id.btn_request_single_permission);
        mBtnRequestMultiPermission = (Button) findViewById(R.id.btn_request_multi_permission);

        mBtnRequestSinglePermission.setOnClickListener(this);
        mBtnRequestMultiPermission.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_request_multi_permission:
                EPermission.requestPermissions(this, REQUEST_MULTI_PERMISSON, Manifest.permission.CAMERA, Manifest.permission.READ_CONTACTS);
                break;
            case R.id.btn_request_single_permission:
                // judge the request permission should be show the rational explain to the user
                if (!EPermission.shouldShowRequestPermissionRationale(this, REQUEST_SINGLE_PERMISSON, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    EPermission.requestPermissions(this, REQUEST_SINGLE_PERMISSON, Manifest.permission.READ_EXTERNAL_STORAGE);
                }
                break;
        }
    }

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

    /**
     * below content is process READ_EXTERNAL_STORAGE permission callback
     */

    @PermissionGrant(requestPermission = Manifest.permission.READ_EXTERNAL_STORAGE)
    public void grantSdcardPermission() {
        Log.d(TAG, "permission READ_EXTERNAL_STORAGE grant");
        Toast.makeText(this, "permission READ_EXTERNAL_STORAGE grant", Toast.LENGTH_SHORT).show();
    }

    @PermissionDeny(requestCode = REQUEST_SINGLE_PERMISSON, requestPermission = Manifest.permission.READ_EXTERNAL_STORAGE)
    public void denyGrantSdcardPermission() {
        Log.d(TAG, "permission READ_EXTERNAL_STORAGE deny");
        Toast.makeText(this, "permission READ_EXTERNAL_STORAGE deny", Toast.LENGTH_SHORT).show();
    }

    @PermissionRationale(requestCode = REQUEST_SINGLE_PERMISSON, requestPermission = Manifest.permission.READ_EXTERNAL_STORAGE)
    public void rationableSdcardPermission() {
        Log.d(TAG, "ask to rationable READ_EXTERNAL_STORAGE permission");
        Toast.makeText(this, "ask to rationable READ_EXTERNAL_STORAGE permission", Toast.LENGTH_SHORT).show();
        EPermission.requestPermissions(this, REQUEST_SINGLE_PERMISSON, Manifest.permission.READ_EXTERNAL_STORAGE);
    }


    /**
     * below content is process CAMERA permission callback
     */

    @PermissionGrant(requestPermission = Manifest.permission.CAMERA)
    public void grantCameraPermission() {
        Log.d(TAG, "permission CAMERA grant");
        Toast.makeText(this, "permission CAMERA grant", Toast.LENGTH_SHORT).show();
    }

    @PermissionDeny(requestPermission = Manifest.permission.CAMERA)
    public void denyGrantCameraPermission() {
        Log.d(TAG, "permission CAMERA deny");
        Toast.makeText(this, "permission CAMERA deny", Toast.LENGTH_SHORT).show();
    }


    @PermissionRationale(requestPermission = Manifest.permission.CAMERA)
    public void rationableCameraPermission() {
        Log.d(TAG, "ask to rationable CAMERA permission");
        Toast.makeText(this, "ask to rationable CAMERA permission", Toast.LENGTH_SHORT).show();
        EPermission.requestPermissions(this, REQUEST_SINGLE_PERMISSON, Manifest.permission.CAMERA);
    }


    /**
     * below content is process READ_CONTACTS permission callback
     */

    @PermissionGrant(requestCode = REQUEST_MULTI_PERMISSON, requestPermission = Manifest.permission.READ_CONTACTS)
    public void grantContactPermission() {
        Log.d(TAG, "permission READ_CONTACTS grant");
        Toast.makeText(this, "permission READ_CONTACTS grant", Toast.LENGTH_SHORT).show();
    }

    @PermissionDeny(requestCode = REQUEST_MULTI_PERMISSON, requestPermission = Manifest.permission.READ_CONTACTS)
    public void denyGrantContactPermission() {
        Log.d(TAG, "permission READ_CONTACTS deny");
        Toast.makeText(this, "permission READ_CONTACTS deny", Toast.LENGTH_SHORT).show();
    }

    @PermissionRationale(requestCode = REQUEST_MULTI_PERMISSON, requestPermission = Manifest.permission.READ_CONTACTS)
    public void rationableContactPermission() {
        Log.d(TAG, "ask to rationable READ_CONTACTS permission");
        Toast.makeText(this, "ask to rationable READ_CONTACTS permission", Toast.LENGTH_SHORT).show();
        EPermission.requestPermissions(this, REQUEST_SINGLE_PERMISSON, Manifest.permission.READ_CONTACTS);
    }

}

在Fragment中使用

public class PermissionFragment extends Fragment implements View.OnClickListener {

    private static final String TAG = "permission";

    private static final int REQUEST_SINGLE_PERMISSON = 1;
    private static final int REQUEST_MULTI_PERMISSON = 2;

    private View view;

    Button mBtnRequestSinglePermission;
    Button mBtnRequestMultiPermission;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.fragment_permission, container, false);
        initView();
        return view;
    }

    private void initView() {
        mBtnRequestSinglePermission = (Button) view.findViewById(R.id.btn_request_single_permission);
        mBtnRequestMultiPermission = (Button) view.findViewById(R.id.btn_request_multi_permission);

        mBtnRequestSinglePermission.setOnClickListener(this);
        mBtnRequestMultiPermission.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_request_multi_permission:
                EPermission.requestPermissions(this, REQUEST_MULTI_PERMISSON, Manifest.permission.CAMERA, Manifest.permission.READ_CONTACTS);
                break;
            case R.id.btn_request_single_permission:
                // judge the request permission should be show the rational explain to the user
                if (!EPermission.shouldShowRequestPermissionRationale(this, REQUEST_SINGLE_PERMISSON, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    EPermission.requestPermissions(this, REQUEST_SINGLE_PERMISSON, Manifest.permission.READ_EXTERNAL_STORAGE);
                }
                break;
        }
    }

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

    /**
     * below content is process READ_EXTERNAL_STORAGE permission callback
     */

    @PermissionGrant(requestPermission = Manifest.permission.READ_EXTERNAL_STORAGE)
    public void grantSdcardPermission() {
        Log.d(TAG, "permission READ_EXTERNAL_STORAGE grant");
        Toast.makeText(getActivity(), "permission READ_EXTERNAL_STORAGE grant", Toast.LENGTH_SHORT).show();
    }

    @PermissionDeny(requestCode = REQUEST_SINGLE_PERMISSON, requestPermission = Manifest.permission.READ_EXTERNAL_STORAGE)
    public void denyGrantSdcardPermission() {
        Log.d(TAG, "permission READ_EXTERNAL_STORAGE deny");
        Toast.makeText(getActivity(), "permission READ_EXTERNAL_STORAGE deny", Toast.LENGTH_SHORT).show();
    }

    @PermissionRationale(requestCode = REQUEST_SINGLE_PERMISSON, requestPermission = Manifest.permission.READ_EXTERNAL_STORAGE)
    public void rationableSdcardPermission() {
        Log.d(TAG, "ask to rationable READ_EXTERNAL_STORAGE permission");
        Toast.makeText(getActivity(), "ask to rationable READ_EXTERNAL_STORAGE permission", Toast.LENGTH_SHORT).show();
        EPermission.requestPermissions(this, REQUEST_SINGLE_PERMISSON, Manifest.permission.READ_EXTERNAL_STORAGE);
    }


    /**
     * below content is process CAMERA permission callback
     */

    @PermissionGrant(requestPermission = Manifest.permission.CAMERA)
    public void grantCameraPermission() {
        Log.d(TAG, "permission CAMERA grant");
        Toast.makeText(getActivity(), "permission CAMERA grant", Toast.LENGTH_SHORT).show();
    }

    @PermissionDeny(requestPermission = Manifest.permission.CAMERA)
    public void denyGrantCameraPermission() {
        Log.d(TAG, "permission CAMERA deny");
        Toast.makeText(getActivity(), "permission CAMERA deny", Toast.LENGTH_SHORT).show();
    }


    @PermissionRationale(requestPermission = Manifest.permission.CAMERA)
    public void rationableCameraPermission() {
        Log.d(TAG, "ask to rationable CAMERA permission");
        Toast.makeText(getActivity(), "ask to rationable CAMERA permission", Toast.LENGTH_SHORT).show();
        EPermission.requestPermissions(this, REQUEST_SINGLE_PERMISSON, Manifest.permission.CAMERA);
    }


    /**
     * below content is process READ_CONTACTS permission callback
     */

    @PermissionGrant(requestCode = REQUEST_MULTI_PERMISSON, requestPermission = Manifest.permission.READ_CONTACTS)
    public void grantContactPermission() {
        Log.d(TAG, "permission READ_CONTACTS grant");
        Toast.makeText(getActivity(), "permission READ_CONTACTS grant", Toast.LENGTH_SHORT).show();
    }

    @PermissionDeny(requestCode = REQUEST_MULTI_PERMISSON, requestPermission = Manifest.permission.READ_CONTACTS)
    public void denyGrantContactPermission() {
        Log.d(TAG, "permission READ_CONTACTS deny");
        Toast.makeText(getActivity(), "permission READ_CONTACTS deny", Toast.LENGTH_SHORT).show();
    }

    @PermissionRationale(requestCode = REQUEST_MULTI_PERMISSON, requestPermission = Manifest.permission.READ_CONTACTS)
    public void rationableContactPermission() {
        Log.d(TAG, "ask to rationable READ_CONTACTS permission");
        Toast.makeText(getActivity(), "ask to rationable READ_CONTACTS permission", Toast.LENGTH_SHORT).show();
        EPermission.requestPermissions(this, REQUEST_SINGLE_PERMISSON, Manifest.permission.READ_CONTACTS);
    }
}

小心踩坑

  • @PermissionGrant,@PermissionDeny and @PermissionRationale有一个requestCode属性,如果你没有指定其值,那么该注解对应的方法可以接收所有的关于requestPermission的回调

  • 对于一次性申请多个权限的操作,会针对用户对每个权限授权与否,回调不同的注解方法

  • 如果在申请权限的弹出框中,用户勾选了“禁止后不再询问”选项,或者在系统的权限管理界面禁止了该权限后,那么后续无论何时再申请权限,都会直接回调@PermissionDeny注解方法

  • 如果用户同意了某个权限的申请后,后续再次调用权限申请,都会直接回调@PermissionGrant注解方法

总结

  • 项目已经发布在github上(EPermission),想看源码的小伙伴,可以clone下来看看

  • 项目采用了基于Annotation Processing Tool技术,关于这个技术的详细解释,可以参考这篇Annotation-Processing-Tool详解