请问我该如何向用户解释权限问题?
我正在使用Camera2API,并实现了以下代码片段来动态请求权限:
private void openCamera(int width, int height) {
setUpCameraOutputs(width, height);
CameraHelper.configureTransform(width, height, textureView, previewSize, getActivity());
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
if (!cameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(
getActivity(), new String[]{android.Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST);
// MY_PERMISSIONS_REQUEST is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
}
}
根据Google文档,我应该先检查用户是否拒绝了权限。有一个方法可返回真或假。
shouldShowRequestPermissionRationale();
根据 Google 的描述:
如果该应用程序之前请求过此权限但用户拒绝了请求,则此方法返回 true。
如果我正确理解的话,该方法实现中的注释说明如下:
// 异步向用户显示解释,不要阻塞此线程等待用户响应!
// 用户看到解释后,请尝试再次请求权限。
例如,如果用户在之前拒绝了我的权限请求,下一次当他进入我的相机应用程序界面时,我应该创建一个自定义弹窗,并说明“如果您想继续,请同意此权限”。例如,如果用户这一次同意了,我应该再次调用该方法,按照以下说明:
// 用户看到解释后,请尝试再次请求权限。
但是,该方法
shouldShowRequestPermissionRationale();
因为它不知道用户意图同意权限,所以请再次向我返回true
。
你能否向我解释如何正确实现呢?也许您有例子?