权限未授予

时间:2019-01-08 20:18:15

标签: java android-studio android-intent permissions

我正在尝试访问系统设置,但是看来我请求权限的方法总是返回false。我的知识有限,所以我真的不知道从哪里开始修复它

private void getPermissions(){
    boolean value;
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        value = Settings.System.canWrite(getApplicationContext());
          if(value) {
              success = true;
          }else{
              Intent intent = new 
                            Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
              intent.setData(Uri.parse("package:" + 
                            getApplicationContext().getPackageName()));
              startActivity(intent);
           }
      }
}

在创建应用后会调用getPermission()。

1 个答案:

答案 0 :(得分:0)

尝试一下

String[] permissionsRequired = new String{Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO};

//on button click
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if ((ContextCompat.checkSelfPermission(this, permissionsRequired[0])
                            != PackageManager.PERMISSION_GRANTED) ||
                            (ContextCompat.checkSelfPermission(this, permissionsRequired[1])
                                    != PackageManager.PERMISSION_GRANTED) ||
                            (ContextCompat.checkSelfPermission(this, permissionsRequired[2])
                                    != PackageManager.PERMISSION_GRANTED)) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            requestPermission();

                        }
                    }
                }
            }
            break;
    }
}

public void requestPermission() {
    ActivityCompat.requestPermissions((Activity) this, permissionsRequired, REQUEST_PERMISSION_SETTING);
}