Android请求权限回调

时间:2017-05-03 12:58:16

标签: android google-maps

我尝试从https://developers.google.com/android/guides/permissions

实施Android权限

有几个问题: 1)回调onRequestPermissionResult - 参数String []权限和int [] grantResults来自哪里? 2)我一直为grantResults和String []权限获取空值 3)我已将const REQUEST_LOCATION的值设置为0 - 这个const的值是什么?

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
  // Check Permissions Now
  ActivityCompat.requestPermissions(this,
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
        REQUEST_LOCATION);
} else {
  // permission has been granted, continue as usual
  Location myLocation =
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}

一个无关的问题......当获得谷歌地图API密钥时,需要设置SHA-1值或者是可选的,只是将密钥限制为android。

感谢。

public void onRequestPermissionsResult(int requestCode,
                                       String[] permissions,
                                       int[] grantResults) 

4 个答案:

答案 0 :(得分:2)

公共类RequestUserPermission {

private Activity activity;


// Storage Permissions
private static final int REQUEST_PERMISSION = 1;
private static String[] PERMISSIONS = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
};

public RequestUserPermission(Activity activity) {
    this.activity = activity;
}

public  boolean verifyStoragePermissions() {
    // Check if we have write permission
    int permissionWrite = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int permissionRead = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);

    if ((permissionWrite != PackageManager.PERMISSION_GRANTED) ||
            (permissionRead != PackageManager.PERMISSION_GRANTED)) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS,
                REQUEST_PERMISSION
        );
        return false;
    }else{
        return true;
    }
}

public void providePermission(){
    verifyStoragePermissions();
}

public void forceFullOpenPermission(){
    if(!verifyStoragePermissions()){
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
        intent.setData(uri);
        activity.startActivityForResult(intent, REQUEST_PERMISSION);
    }
}

在您的活动中

RequestUserPermission requestUserPermission = new 
RequestUserPermission(DashBoard.this);
    requestUserPermission.verifyStoragePermissions();

如果您想强制开放许可

requestUserPermission.forceFullOpenPermission();

答案 1 :(得分:0)

1)请求代码是您将其命名为REQUEST_LOCATION且值为0的代码。permissions是您发送的请求。例如,您可以一次请求位置权限和读取SMS权限,因此您将获得权限字符串数组。并且grantResults是每个请求的状态,例如,如果用户接受位置请求,它将为1,如果他拒绝读取SMS,则为0

2)我认为这意味着你已拒绝了该请求

3)如果您已经从不同的地方多次请求,您可以确定哪个结果是针对哪个请求

并回答您无关的问题:不可选,如果您不希望其他应用程序使用您的密钥,您必须设置

答案 2 :(得分:0)

1)回调onRequestPermissionResult - 参数String []权限和int [] grantResults来自哪里?

权限是您请求的权限列表,grantResults是这些请求的结果。因此,由于您只请求了一个权限(ACCESS_FINE_LOCATION),您将获得权限中的ACCESS_FINE_LOCATION,以及grantResults中的PackageManager.PERMISSION_GRANTED或PackageManager.PERMISSION_DENIED

2)我一直为grantResults和String []权限

获取空值

这意味着用户未在权限对话框中单击“接受”或“拒绝”。相反,他们通过点击对话框或离开应用程序取消了对话框。

3)我已将const REQUEST_LOCATION的值设置为0 - 这个const的值是什么重要?

不,这可以是任何

答案 3 :(得分:0)

获取此类运行时权限

    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
                    PackageManager.PERMISSION_GRANTED &&
                    ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
                            PackageManager.PERMISSION_GRANTED) {
                    // Permission already Granted
                    //Do your work here
//Perform operations here only which requires permission
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
            }

如果尚未授予权限,则覆盖onRequestPermission结果

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            if (requestCode == 1) {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                            //Permission Granted
                            //Do your work here
//Perform operations here only which requires permission
                }
            }
        }