Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 棉花糖许可和解释_Java_Android_Permissions_Camera_Android Camera - Fatal编程技术网

Java 棉花糖许可和解释

Java 棉花糖许可和解释,java,android,permissions,camera,android-camera,Java,Android,Permissions,Camera,Android Camera,可以向我解释我应该如何向用户解释权限吗 我和Camera2API一起工作,我实现了这样一段代码,以请求许可证 private void openCamera(int width, int height) { setUpCameraOutputs(width, height); CameraHelper.configureTransform(width, height, textureView, previewSize, getActivity()); CameraMana

可以向我解释我应该如何向用户解释权限吗

我和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);
    }
}
根据GoogleDocks的说法,我应该检查用户之前是否拒绝许可

有一种方法可以返回true或false

shouldShowRequestPermissionRationale();
根据谷歌的说法

如果应用程序以前请求过此权限,并且用户拒绝了该请求,则此方法返回true

如果我理解正确,根据谷歌的评论在这个方法实现

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

//此线程正在等待用户的响应!在用户之后

//请查看说明,然后重试请求权限

最后,例如,用户在使用摄像头进入我的屏幕之前和下一次拒绝我的许可时,应用程序应创建“我的castom”弹出窗口,并解释“如果您想继续,请同意此许可”,例如,用户这次同意,我应根据此方法再次调用此方法

//请查看说明,然后重试请求权限

但是这种方法

shouldShowRequestPermissionRationale();
再次返回给我
true
,因为它不知道用户同意权限的意图


你能给我解释一下怎么做吗?也许你有这样的例子?

嘿,你可以使用一些库,它们非常适合在应用程序启动时询问用户权限

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

您可以找到权限调度器库的使用示例

您还可以检查以下库:


最后,我在@numeoi的帮助下找到了一个解决方案,非常感谢

然后像这样实现它

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
    }
}
试试这个,可能有用