Android请求运行时权限无效

时间:2018-06-12 00:53:52

标签: java android permissions

尝试在Android模拟器上请求运行时权限时遇到了一些问题。我想要做的是点击按钮,请求读取和写入外部存储权限,然后再转到我的异步任务类。

这是我的代码:

private static final int PERMISSION_REQUEST_CODE = 1;
String readPermission = Manifest.permission.READ_EXTERNAL_STORAGE;
String writePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
@Click(R.id.buttonVersionUpgrade)
void buttonVersionUpgradeClicked(View v) {

    if (!checkPermission(readPermission) || !checkPermission(writePermission)) {
        requestPermission(readPermission);
        requestPermission(writePermission);
    }
   // async task here
}

private boolean checkPermission(String permission){
    if (Build.VERSION.SDK_INT >= 23) {
        int result = ContextCompat.checkSelfPermission(getApplicationContext(), permission);
        if (result == PackageManager.PERMISSION_GRANTED){
            return true;
        } else {
            return false;
        }
    } else {
        return true;
    }
}

private void requestPermission(String permission){
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)){
        Toast.makeText(getApplicationContext(), "Read/Write external storage permission allows us to read/write data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
    }
    ActivityCompat.requestPermissions(this, new String[]{permission},PERMISSION_REQUEST_CODE);
}

@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. Now you can read/write data.",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),"Permission Denied. You cannot read/write data.",
                        Toast.LENGTH_LONG).show();
            }
            break;
    }
}

但是,在移动到异步任务执行之前,它不会提示用户获得权限,这会导致应用程序崩溃。它只会在应用程序崩溃时提示用户。有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

这是因为ActivityCompat.requestPermissions不是同步调用。操作系统将显示弹出窗口(实际上是一个活动),后面的语句将被执行。

由于这两个权限对您的请求同时是强制性的。

 if (!checkPermission(readPermission) || !checkPermission(writePermission)) {
        requestPermission(new String[] {readPermission,writePermission});        
  } else{
    //Run Async task directly since the permissions are already granted
  }



private void requestPermission(String[] permissions){
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])||ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[1])){
        Toast.makeText(getApplicationContext(), "Read/Write external storage permission allows us to read/write data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
    }
    ActivityCompat.requestPermissions(this, permissions,PERMISSION_REQUEST_CODE);
}

ActivityCompat.requestPermissions(this,permissions,requestCode);

AsyncTask执行移至onRequestPermissionsResult,如下所示:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (verifyGrantResults(grantResults)) {
                Toast.makeText(getApplicationContext(), "Permission Granted. Now you can read/write data.",
                        Toast.LENGTH_LONG).show();
                        //start Async Task here
            } else {
                Toast.makeText(getApplicationContext(),"Permission Denied. You cannot read/write data.",
                        Toast.LENGTH_LONG).show();
            }
            break;
    }
}

private boolean verifyGrantResults(final int[] grantResults) {
        boolean permissionsGranted = true;
        if (grantResults.length > 0) {
            for (int result : grantResults) {
                if (result == PackageManager.PERMISSION_DENIED) {
                    permissionsGranted = false;
                }
            }
            return permissionsGranted;
        }
        return false;
    }