ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
1);
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
2);
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CALL_PHONE},
3);
在这里,我拥有我想要的三个权限。但是,当弹出应用程序时,只会弹出三个权限中的一个。如何获得所有三个权限请求?
答案 0 :(得分:1)
这是一个异步调用,可能有很多调用。因此,应该有一个关键,让你区分它们。你将在
中使用它@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// Your code here
}
}
}
在这种情况下,MY_PERMISSIONS_REQUEST_READ_CONTACTS是一个int值,您可以自由定义它。例如:
int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 999;
答案 1 :(得分:0)
尝试使用此代码:
public void requestAppPermissions(final String[] requestedPermissions,
final int requestCode, setPermissionListener listener) {
this.permissionListener = listener;
int permissionCheck = PackageManager.PERMISSION_GRANTED;
for (String permission : requestedPermissions) {
permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
}
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
} else {
if (permissionListener != null) permissionListener.onPermissionGranted(requestCode);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) {
if (permissionListener != null) permissionListener.onPermissionGranted(requestCode);
break;
} else if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
if (permissionListener != null) permissionListener.onPermissionDenied(requestCode);
break;
} else {
if (permissionListener != null)
permissionListener.onPermissionNeverAsk(requestCode);
break;
}
}
}
public interface setPermissionListener {
void onPermissionGranted(int requestCode);
void onPermissionDenied(int requestCode);
void onPermissionNeverAsk(int requestCode);
}
<强>调用:强>
private void askPermission() {
requestAppPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION},
Constants.PERMISSION_STORAGE,
new setPermissionListener() {
@Override
public void onPermissionGranted(int requestCode) {
selectImageDialog();
}
@Override
public void onPermissionDenied(int requestCode) {
showSnackbar(mBinder.getRoot(), getString(R.string.critical_permission_denied),
Snackbar.LENGTH_INDEFINITE, getString(R.string.allow), new OnSnackbarActionListener() {
@Override
public void onAction() {
askPermission();
}
});
}
@Override
public void onPermissionNeverAsk(int requestCode) {
showPermissionSettingDialog(getString(R.string.need_permission));
}
});
}
答案 2 :(得分:0)
您也可以使用 Array 来存储所需的权限,然后使用所有权限调用一个请求。
fun verifyPermissions(activity: Activity?) {
var permissions = mutableListOf<String>()
// Check if we have SEND_SMS permission
if (ActivityCompat.checkSelfPermission(
activity!!,
Manifest.permission.SEND_SMS
) != PackageManager.PERMISSION_GRANTED
) {
permissions.add(Manifest.permission.READ_CONTACTS)
} else {
// permission granted
}
if (ActivityCompat.checkSelfPermission(
activity,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
permissions.add(Manifest.permission.READ_CALL_LOG)
} else {
// permission granted
}
// continue to add other permissions
if (!permissions.isEmpty()) {
ActivityCompat.requestPermissions(
activity, permissions.toTypedArray(),
REQUEST_CODE
)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
var counter: Int = 0
when (requestCode) {
REQUEST_CODE -> if ((grantResults.isNotEmpty())) {
while (counter < NUM_OF_PERM) {
if (grantResults[counter++] == PackageManager.PERMISSION_GRANTED) {
// permission granted
}
}
}
}
}