以下代码具有多个权限,我的错误是在点击按钮时它只询问一个权限,再次点击意味着它要求另一个权限。
我的Onclick
public void onClick(View v) {
getPermissionCamara();
getStroagePermission();
selectImage();
}
我的权限就是这样
private static final int CAMERA_PERMISSIONS_REQUEST = 1;
private static final int STROAGE_PERMISSIONS_REQUEST = 2;
public void getPermissionCamara() {
if (ContextCompat.checkSelfPermission(this, CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(
CAMERA)) {
}
requestPermissions(new String[]{CAMERA},CAMERA_PERMISSIONS_REQUEST);
}
}
public void getStroagePermission() {
if (ContextCompat.checkSelfPermission(this,READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(
READ_EXTERNAL_STORAGE)) {
}
requestPermissions(new String[]{READ_EXTERNAL_STORAGE},STROAGE_PERMISSIONS_REQUEST);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults) {
// Make sure it's our original READ_CONTACTS request
if (requestCode == CAMERA_PERMISSIONS_REQUEST) if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Camera permission granted", Toast.LENGTH_SHORT).show();
}
else if (requestCode == STROAGE_PERMISSIONS_REQUEST) if (grantResults.length == 2 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:4)
requestPermissions(new String[]{
Manifest.permission.READ_CONTACTS,
Manifest.permission.ACCESS_FINE_LOCATION},
ASK_MULTIPLE_PERMISSION_REQUEST_CODE);
答案 1 :(得分:2)
我这样做了:
public static final int MULTIPLE_PERMISSION_REQUEST = 43;
public boolean requestPermission(String... permissions) {
final List<String> permissionsList = new ArrayList<>();
for (String perm : permissions) {
addPermission(permissionsList, perm);
}
if (permissionsList.size() > 0) {
if (Utils.hasMarshmallow())
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
MULTIPLE_PERMISSION_REQUEST);
else
ActivityCompat.requestPermissions(this, permissionsList.toArray(new String[permissionsList.size()]),
MULTIPLE_PERMISSION_REQUEST);
return false;
} else
return true;
}
private boolean addPermission(List<String> permissionsList, String permission) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (Utils.hasMarshmallow())
if (!shouldShowRequestPermissionRationale(permission))
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
/*switch (requestCode) {
case PERMISSION_REQUEST:
case MULTIPLE_PERMISSION_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
}
}*/
}