项目适配Android6.0,多个权限一起请求处理

升级到23以后就需要对Android6.0进行适配,适配的重点就是对权限进行适配,
权限分为一般权限和危险权限,项目如果涉及危险权限就需要进行适配。
下面主要涉及多个权限一起请求的处理

 private void checkPermision() {
     String[]  mPermissions = new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
        List deniedPermissions = findDeniedPermissions(mPermissions);
        if (deniedPermissions != null && deniedPermissions.size() > 0) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    deniedPermissions.get(0))) {
                //如果拒绝过,跳转到设置页面
                jump2Setting();
            } else {
                //申请多个对应的权限
                ActivityCompat.requestPermissions(this,
                        deniedPermissions.toArray(new String[]{}),
                        REQUECT_CODE_SDCARD);
            }
        } else {
       //权限允许后自己的操作
            Toast.makeText(PermisionActivity.this, "获取权限成功", Toast.LENGTH_SHORT).show();
        }
    }
  //获取没有允许的权限
    private List findDeniedPermissions(String[] permissions) {
        List list = new ArrayList<>();
        for (String str : permissions) {
            if (ContextCompat.checkSelfPermission(this,
                    str) != PackageManager.PERMISSION_GRANTED) {
                list.add(str);
            }
        }
        return list;
    }
  @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUECT_CODE_SDCARD:
                int permission = 0;
                for (int i = 0; i < permissions.length; i++) {
                    for (int j = 0; j < mPermissions.length; j++) {
                        if (mPermissions[j].equals(permissions[i])) {
                            if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                                permission++;
                            }
                        }
                    }
                }
                if (permission == permissions.length) {
                      //请求的权限都允许了的操作
                    Toast.makeText(PermisionActivity.this, "获取权限成功", Toast.LENGTH_SHORT).show();
                } else {
                //请求的多个权限如果存在没有运行的情况
                    Toast.makeText(PermisionActivity.this, "允许权限后才能使用该功能", Toast.LENGTH_SHORT).show();
                }
                break;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //如果是从设置界面返回,就继续判断权限
        if (requestCode == REQUECT_CODE_SETTING) {
            checkPermision();
        }
    }

你可能感兴趣的:(项目适配Android6.0,多个权限一起请求处理)