Android在Android 6.0中不显示RECORD_AUDIO的权限对话框 [英] Android does not show permission dialog for RECORD_AUDIO in android 6.0
问题描述
Android不显示RECORD_AUDIO的权限对话框
Android does not show permission dialog for RECORD_AUDIO
我已在android清单中添加了所需的权限
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" />
和以下代码
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);
}
}
并进行检查
if (!checkPermission()) {
requestPermission();
}
无论如何,没有显示询问权限的对话框或询问权限的屏幕,
however the dialog to ask permission or the screen asking for permission is not shown ,
我正在模拟器上的android 6.0上进行测试
I am testing on android 6.0 on an emulator
推荐答案
ActivityCompat.shouldShowRequestPermissionRationale()
方法仅指示是否显示消息.无论返回值是什么,都应请求权限.
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);
}
}
这篇关于Android在Android 6.0中不显示RECORD_AUDIO的权限对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!