棉花糖权限及解释

4

请问我该如何向用户解释权限问题?

我正在使用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

你能否向我解释如何正确实现呢?也许您有例子?


尝试一下,它可能有效。https://dev59.com/J1wX5IYBdhLWcg3w-Thv#41221852 - Bipin Bharti
2个回答

8

最终在@nuuneoi的帮助下,我找到了解决方案,非常感谢!

这是实现方式:

public void camera(View view) {
    toCamera();
}

private void toCamera() {
    if (!isCheckPermission()){
        return;
    }

    if (isProcessWasFinish()) {
        startActivity(new Intent(getApplicationContext(), CameraActivity.class));
        overridePendingTransition(R.anim.open_next, R.anim.close_main);
    } else {
        startActivity(new Intent(getApplicationContext(), UserDataScreen.class));
        overridePendingTransition(R.anim.open_next, R.anim.close_main);
    }
}

private boolean isCheckPermission() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
            PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
            showMessageOKCancel("You need to allow access to Camera");
            return false;
        }

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA},
                MY_PERMISSIONS_REQUEST);
        return false;
        // MY_PERMISSIONS_REQUEST is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }

    return true;
}

private void showMessageOKCancel(String message) {
    new AlertDialog.Builder(MainActivity.this)
            .setMessage(message)
            .setPositiveButton("OK", listener)
            .setNegativeButton("Cancel", listener)
            .create()
            .show();
}

DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {

    final int BUTTON_NEGATIVE = -2;
    final int BUTTON_POSITIVE = -1;

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case BUTTON_NEGATIVE:
                // int which = -2
                dialog.dismiss();
                break;

            case BUTTON_POSITIVE:
                // int which = -1
                ActivityCompat.requestPermissions(
                        MainActivity.this, new String[]{android.Manifest.permission.CAMERA},
                        MY_PERMISSIONS_REQUEST);
                dialog.dismiss();
                break;
        }
    }
};

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.e(MY_LOG, "Camera permission Granted");
                // permission was granted, yay! Do the
                // contacts-related task you need to do.

                toCamera();

            } else {
                Log.e(MY_LOG, "Camera permission Denied");
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
        }
        default: {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接