在Android活动中没有回调onRequestPermissionsResult

时间:2016-10-18 22:16:28

标签: android permissions android-6.0-marshmallow

我正在尝试在启动相机活动之前获得用户的相机许可。在用户按下"允许"之后,onRequestPermissionsResult永远不会被回调。来自权限对话框。这是我的Activity类:

std::cout

这是我的相机许可的App Manifest:

public class ImageCaptureActivity extends AppCompatActivity {

    public static final String TAG = ImageCaptureActivity.class.getSimpleName();

    private static final int REQUEST_CAMERA = 0;
    private static final int REQUEST_CAMERA_PERMISSION = 1;
    private Point mSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_capture);

        Display display = getWindowManager().getDefaultDisplay();
        mSize = new Point();
        display.getSize(mSize);

        // Request for Camera Permission
        requestForCameraPermission(findViewById(android.R.id.content));
    }

    /**
     * @param view
     * @brief requestForCameraPermission
     */
    public void requestForCameraPermission(View view) {
        Log.v(TAG, "Requesting Camera Permission");

        final String permission = Manifest.permission.CAMERA;
        if (ContextCompat.checkSelfPermission(ImageCaptureActivity.this, permission)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(ImageCaptureActivity.this, permission)) {
                showPermissionRationaleDialog(getString(R.string.camera_permission_rationale), permission);
            } else {
                requestForPermission(permission);
            }
        } else {
            launch();
        }
    }

    /**
     * @param message
     * @param permission
     * @brief showPermissionRationaleDialog
     */
    private void showPermissionRationaleDialog(final String message, final String permission) {
        new AlertDialog.Builder(ImageCaptureActivity.this)
                .setMessage(message)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ImageCaptureActivity.this.requestForPermission(permission);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .create()
                .show();
    }

    /**
     * @param permission
     * @brief requestForPermission
     */
    private void requestForPermission(final String permission) {
        ActivityCompat.requestPermissions(ImageCaptureActivity.this, new String[]{permission}, REQUEST_CAMERA_PERMISSION);
    }

    /**
     * @brief launch
     */
    private void launch() {
        Log.v(TAG, "Camera Permission Granted, launching the CameraActivity");
        String documentId = getIntent().getStringExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID);
        Intent startCustomCameraIntent = new Intent(this, CameraActivity.class);
        startCustomCameraIntent.putExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID, documentId);
        startActivityForResult(startCustomCameraIntent, REQUEST_CAMERA);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CAMERA_PERMISSION:
                final int numOfRequest = grantResults.length;
                final boolean isGranted = numOfRequest == 1
                        && PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
                Log.v(TAG, "Camera Permission callback on onRequestPermissionsResult");
                if (isGranted) {
                    launch();
                }
                break;

            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

我的Gradle依赖度:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.testpackage">

    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature
        android:name="android.hardware.camera.front"
        android:required="true" />
    <uses-feature android:name="android.hardware.camera2" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application

1 个答案:

答案 0 :(得分:6)

我自己尝试了你的代码,一切正常。我认为唯一可能出现问题的是你的活动有这两个标志的情景:

*

这是一个解释问题的人:https://stackoverflow.com/a/35772265/5281581