美文网首页
【Susen】EasyPermissions

【Susen】EasyPermissions

作者: 毕加森 | 来源:发表于2018-11-17 15:55 被阅读0次

    https://github.com/googlesamples/easypermissions

    dependencies {
        // 运行时权限
        implementation 'pub.devrel:easypermissions:1.0.1'
    }
    
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    
    • 实现权限回调接口
    implements EasyPermissions.PermissionCallbacks
    
    • 使用方法
    private static final int PRC_CALENDAR = 0;// 权限请求码:日历
    
    @AfterPermissionGranted(PRC_CALENDAR)
    private void requestPermissions() {// 此方法不能带参数
        String[] perms = {Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR};
        // EasyPermissions方法:检测权限
        if (EasyPermissions.hasPermissions(this, perms)) {
            Toast.makeText(this, "正在打开系统日历...", Toast.LENGTH_SHORT).show();// TODO
        } else {
            // EasyPermissions方法:请求权限
            EasyPermissions.requestPermissions(this, "请授予「日历」所需要的相关权限!", PRC_CALENDAR, perms);
        }
    }
    
    // 请求权限结果回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // EasyPermissions方法:处理请求权限结果
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
    
    // EasyPermissions回调:权限被授予
    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
    }
    
    // EasyPermissions回调:权限被拒绝
    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        if (requestCode == PRC_CALENDAR) {
            Toast.makeText(this, "您拒绝了「日历」所需要的相关权限!", Toast.LENGTH_SHORT).show();
        }
    }
    

    相关文章

      网友评论

          本文标题:【Susen】EasyPermissions

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