使用easypermission进行权限请求

2018-09-13  本文已影响0人  _Youngjim

https://github.com/googlesamples/easypermissions

实现接口EasyPermissions.PermissionCallbacks

 @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {

    }
@Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        Toast.makeText(this, "没有相关权限!", Toast.LENGTH_SHORT).show();
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            if (requestCode == CALL_PHONE) {
                //拨打电话权限
                new AppSettingsDialog.Builder(this).setTitle("权限申请").setRationale("请授予拨打电话权限").build().show();
            } else {
                new AppSettingsDialog.Builder(this).build().show();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // Forward results to EasyPermissions
        try {
            EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

调用方法

private static final int CALL_PHONE = 100;

@AfterPermissionGranted(CALL_PHONE)
    private void callService() {
        String[] permissions = new String[]{Manifest.permission.CALL_PHONE};
        if (EasyPermissions.hasPermissions(this, permissions)) {
                    CommonUtil.callPhone(AboutActivity.this, aboutUsEntity.getTelephone());
        } else {
            EasyPermissions.requestPermissions(this, "请授予拨打电话权限", CALL_PHONE, permissions);
        }

    }
上一篇下一篇

猜你喜欢

热点阅读