Android does not show permission dialog for RECORD_AUDIO in android 6.0

会有一股神秘感。 提交于 2019-12-11 02:27:16

问题


Android does not show permission dialog for RECORD_AUDIO

I have added the required permission in android manifest

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

and following code

private boolean checkPermission() {
        int result = ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO);
        if (result == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

private void requestPermission() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.RECORD_AUDIO)) {
 Toast.makeText(this, "RECORD AUDIO Permission is required.",
                    Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.RECORD_AUDIO},
                    PERMISSION_REQUEST_CODE);

        }
    }

and to check it

if (!checkPermission()) {
                requestPermission();
            }

however the dialog to ask permission or the screen asking for permission is not shown ,

I am testing on android 6.0 on an emulator


回答1:


ActivityCompat.shouldShowRequestPermissionRationale() method only signals if a message should be shown. You should request permission regardless of its return value.

Take a look at line 142 in this sample: https://developer.android.com/samples/RuntimePermissions/src/com.example.android.system.runtimepermissions/MainActivity.html#l142

/**
 * Requests the Camera permission.
 * If the permission has been denied previously, a SnackBar will prompt the user to grant the
 * permission, otherwise it is requested directly.
 */
private void requestCameraPermission() {
    Log.i(TAG, "CAMERA permission has NOT been granted. Requesting permission.");

    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.CAMERA)) {
        // Provide an additional rationale to the user if the permission was not granted
        // and the user would benefit from additional context for the use of the permission.
        // For example if the user has previously denied the permission.
        Log.i(TAG,
                "Displaying camera permission rationale to provide additional context.");
        Snackbar.make(mLayout, R.string.permission_camera_rationale,
                Snackbar.LENGTH_INDEFINITE)
                .setAction(R.string.ok, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.CAMERA},
                                REQUEST_CAMERA);
                    }
                })
                .show();
    } else {

        // Camera permission has not been granted yet. Request it directly.
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
                REQUEST_CAMERA);
    }
}


来源:https://stackoverflow.com/questions/40947466/android-does-not-show-permission-dialog-for-record-audio-in-android-6-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!