棉花糖权限及说明

Marshmallow permissions and explanation

请向我解释我应该如何向用户许可解释

我使用 Camera2API 并实现了这样的代码片段来动态询问 permishion

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 docks,我应该在

之前检查用户是否拒绝许可

有方法 return true of false

shouldShowRequestPermissionRationale();

并根据 google

This method returns true if the app has requested this permission previously and the user denied the request.

如果我根据此方法实现中的 google 评论理解正确

// 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.

最后,例如,用户在使用相机应用程序进入我的屏幕之前和下一次拒绝我的许可,应该创建带有解释的我的 castom 弹出窗口,例如,这次用户同意,我应该根据这个

再回忆一下这个方法

// sees the explanation, try again to request the permission.

但是这个方法

shouldShowRequestPermissionRationale();

return 再次给我 true,因为它不知道用户同意许可的意图。

你能解释一下如何正确地制作它吗?也许你有例子?

嘿,您可以使用一些非常适合在应用启动时询问用户权限的库。

Android 中有一个很棒的库可以帮助您做到这一点:

Github link for the Permission dispatcher library

您可以找到权限调度程序库的使用示例here

您还可以查看这些库:

App-Runtime-Permissions-Android

Assent

MarshmallowPermissionManager

最终我在@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
    }
}