2
我試圖請求Android 6.0的運行時權限。 這是我的代碼:權限READ_EXTERNAL_STORAGE總是被拒絕
private void enablePermission() {
String[] PERMISSIONS = { Manifest.permission.READ_EXTERNAL_STORAGE};
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS,
PERMISSION_ALL);
}
}
public static boolean hasPermissions(Context context, String... permissions) {
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(context, permission) !=
PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[]
permissions, int[] grantResults) {
switch (requestCode) {
case PERMISSION_ALL:
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
Log.d("Permission", "Granted");
} else {
Log.d("Permission", "Denied");
}
}
}
它總是告訴我的onRequestPermissionsResult
「權限被拒絕」。我試着用ACCESS_FINE_LOCATION
這個代碼,它的工作。