棉花糖的权限和解释

Ale*_*nko 4 java permissions camera android android-camera

可以解释一下我应该如何向用户许可解释

我使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

根据谷歌码头我应该检查用户之前是否拒绝许可

有返回false的方法

shouldShowRequestPermissionRationale();
Run Code Online (Sandbox Code Playgroud)

并根据谷歌

如果应用先前已请求此权限且用户拒绝该请求,则此方法返回true.

如果我理解正确的谷歌评论里面这个方法实现

// 异步向用户显示解释- 不要阻止

//这个线程等待用户的回复!用户之后

//看到解释,再次尝试请求权限.

最后,例如用户拒绝我的许可之前和之后,当他带着相机应用程序进入我的屏幕应该创建我的castom弹出窗口,并解释"请同意这种情况,如果你想继续",例如用户同意这个时间和我应该再次回想起这个方法

//看到解释,再次尝试请求权限.

但这种方法

shouldShowRequestPermissionRationale();
Run Code Online (Sandbox Code Playgroud)

true再次回到我身边,因为它不知道用户意图同意许可.

你能帮我解释一下如何正确地做到这一点吗?也许你有榜样?

Ale*_*nko 8

最终我找到了@nuuneoi的halp解决方案,非常感谢!

并像这样实现它

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
    }
}
Run Code Online (Sandbox Code Playgroud)