要申請任何權限,您可以使用下面的代碼並添加您需要的權限。這是你如何通過訪問相關許可
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
任何數據之前,要求他們處理運行時權限,一旦你這樣做,對於API的設備> = 23你會在運行時得到彈出,然後一旦用戶接受許可或拒絕它,你的onRequestPermissionsResult
方法被調用。所以在這裏你將不得不處理你的檢查是否用戶授予應用程序的權限。如果是的話,你可以用你的邏輯繼續
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 0:
boolean isPerpermissionForAllGranted = false;
if (grantResults.length > 0 && permissions.length==grantResults.length) {
for (int i = 0; i < permissions.length; i++){
if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
isPerpermissionForAllGranted=true;
}else{
isPerpermissionForAllGranted=false;
}
}
Log.e("value", "Permission Granted");
} else {
isPerpermissionForAllGranted=true;
Log.e("value", "Permission Denied");
}
if(isPerpermissionForAllGranted){
// do your stuff here
}
break;
}
}
更新1的反應原住民
String[] perms = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
};
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Checking permissions on init
checkPerms();
}
public void checkPerms() {
// Checking if device version > 22 and we need to use new permission model
if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) {
// Checking if we can draw window overlay
if (!Settings.canDrawOverlays(this)) {
// Requesting permission for window overlay(needed for all react-native apps)
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
}
for(String perm : perms){
// Checking each persmission and if denied then requesting permissions
if(checkSelfPermission(perm) == PackageManager.PERMISSION_DENIED){
requestPermissions(perms, PERMISSION_REQ_CODE);
break;
}
}
}
}
// Window overlay permission intent result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
checkPerms();
}
}
// Permission results
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
switch(permsRequestCode){
case PERMISSION_REQ_CODE:
// example how to get result of permissions requests (there can be more then one permission dialog)
// boolean readAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;
// boolean writeAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED;
// checking permissions to prevent situation when user denied some permission
checkPerms();
break;
}
}
加載運行許可,在Android 6.0(API級別23)開始,用戶授予權限的應用程序,而該應用程序正在運行,而不是在他們安裝應用程序時。 [https://developer.android.com/training/permissions/requesting.html] –
[Android棉花糖請求權限?](https://stackoverflow.com/questions/33666071/android-棉花糖請求權限) –
嗨你發現這個問題的解決方案,請按照此鏈接https://stackoverflow.com/questions/39866869/how-to-ask-permission-to-access-gallery-on-android-m/39866945#39866945 –