美文网首页
使用easypermission进行权限请求

使用easypermission进行权限请求

作者: _Youngjim | 来源:发表于2018-09-13 15:59 被阅读0次

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);
        }

    }

相关文章

网友评论

      本文标题:使用easypermission进行权限请求

      本文链接:https://www.haomeiwen.com/subject/tlpgwftx.html