动态权限

Android动态申请权限

1.普通权限

普通权限直接是在清单文件中注册使用即可,在应用安装应用时,会默认获得许可。

2.需要许可的权限
group:android.permission-group.CONTACTS
  permission:android.permission.WRITE_CONTACTS
  permission:android.permission.GET_ACCOUNTS
  permission:android.permission.READ_CONTACTS

group:android.permission-group.PHONE
  permission:android.permission.READ_CALL_LOG
  permission:android.permission.READ_PHONE_STATE
  permission:android.permission.CALL_PHONE
  permission:android.permission.WRITE_CALL_LOG
  permission:android.permission.USE_SIP
  permission:android.permission.PROCESS_OUTGOING_CALLS
  permission:com.android.voicemail.permission.ADD_VOICEMAIL

group:android.permission-group.CALENDAR
  permission:android.permission.READ_CALENDAR
  permission:android.permission.WRITE_CALENDAR

group:android.permission-group.CAMERA
  permission:android.permission.CAMERA

group:android.permission-group.SENSORS
  permission:android.permission.BODY_SENSORS

group:android.permission-group.LOCATION
  permission:android.permission.ACCESS_FINE_LOCATION
  permission:android.permission.ACCESS_COARSE_LOCATION

group:android.permission-group.STORAGE
  permission:android.permission.READ_EXTERNAL_STORAGE
  permission:android.permission.WRITE_EXTERNAL_STORAGE

group:android.permission-group.MICROPHONE
  permission:android.permission.RECORD_AUDIO

group:android.permission-group.SMS
  permission:android.permission.READ_SMS
  permission:android.permission.RECEIVE_WAP_PUSH
  permission:android.permission.RECEIVE_MMS
  permission:android.permission.RECEIVE_SMS
  permission:android.permission.SEND_SMS
  permission:android.permission.READ_CELL_BROADCASTS
通过ADB命令查看所有的危险权限的列表
adb shell pm list permissions -d -g
3.动态权限的获取流程

动态权限_第1张图片

4.Easypermissions简化了运行时权限的申请、结果处理、判断等步骤

Activity中使用:

4.1判断是否有权限、动态的申请单个或多个权限
 if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
            // 已经拥有了权限,可以执行事件
            Toast.makeText(this, "TODO: Camera things", Toast.LENGTH_LONG).show();
        } else {
            // 没有权限,动态的申请权限
            EasyPermissions.requestPermissions(this, getString(R.string.rationale_camera),
                    RC_CAMERA_PERM, Manifest.permission.CAMERA);
        }
4.2实现动态权限获取的回调方法,直接写入Easypermissions的权限回调方法
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // EasyPermissions 直接调用权限请求的结果即可
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
4.3Easypermissions动态权限回调结果
@Override
    public void onPermissionsGranted(int requestCode, List perms) {
        //动态权限的申请获取成功
        Log.d(TAG, "动态权限获取成功:" + requestCode + ":" + perms.size());
    }
    @Override
    public void onPermissionsDenied(int requestCode, List perms) {
        Log.d(TAG, "动态权限获取失败:" + requestCode + ":" + perms.size());
        //(可选)检查用户是否禁止权限和勾选“不再询问”;
        //这将显示一个对话框,指导他们在应用程序启用权限设置。
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this).setRequestCode(requestCode).build().show();
        }
    }
4.4Easypermissions启动系统设置权限页面的回调,需要再次判断是否同意了之前的权限申请
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //从系统权限设置页面回到主页面,判断之前的权限申请是否成功
        if(requestCode==RC_LOCATION_CONTACTS_PERM){
            locationAndContactsTask();
        }
    }

在Fragment中使用

4.5Easypermissions在Fragment中的使用
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // EasyPermissions 直接调用权限请求的结果即可
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @AfterPermissionGranted(RC_SMS_PERM)
    private void smsTask() {
        if (EasyPermissions.hasPermissions(getContext(), Manifest.permission.READ_SMS)) {
            // 已经拥有了权限,可以执行事件
            Toast.makeText(getActivity(), "TODO: SMS things", Toast.LENGTH_LONG).show();
        } else {
            // 没有权限,动态的申请权限
            EasyPermissions.requestPermissions(this, getString(R.string.rationale_sms),
                    RC_SMS_PERM, Manifest.permission.READ_SMS);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, List perms) {
        //动态权限的申请获取成功
        Log.d(TAG, "动态权限获取成功:" + requestCode + ":" + perms.size());
    }

    @Override
    public void onPermissionsDenied(int requestCode, List perms) {
        //动态权限的申请获取失败
        Log.d(TAG, "动态权限获取失败:" + requestCode + ":" + perms.size());
    }
5.参考的资源

GitHub:easypermissions

你可能感兴趣的:(Android)