嘿我正在尝试请求存储访问权限,我注意到在我的其他手机中有android 5.0的权限请求崩溃应用程序。我应该怎么做才能在没有崩溃这个Android版本的应用程序的情况下请求权限,以及我应该从哪个Android版本开始?
这是询问权限的代码:
int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 0;
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Explain to the user why we need to read the contacts
}
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant that should be quite unique
return;
}
答案 0 :(得分:0)
动态权限需要API 23或更高版本,因此您可以在条件检查正在运行的API的版本中包装代码。
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 0;
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if(shouldShowRequestPermissionRationale(
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Explain to the user why we need to read the contacts
}
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant that should be quite unique
return;
}
}