我试图同时要求多个权限,并且我在日志中得到了这个权限:
W / Activity:一次只能获取一组权限
该行出现了很多次,直到我接受授予权限,导致延迟。
以下是我如何询问权限:
private void requestPermissions() {
if (checkingPermissionIsEnabledOrNot()) {
Toast.makeText(getApplicationContext(), "All Permissions Granted Successfully", Toast.LENGTH_LONG).show();
}
// If permission is not enabled then else condition will execute.
else {
//Calling method to enable permission.
RequestMultiplePermission();
}
}
public boolean checkingPermissionIsEnabledOrNot() {
int SecondPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_PHONE_STATE);
int ThirdPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION);
return
SecondPermissionResult == PackageManager.PERMISSION_GRANTED && ThirdPermissionResult == PackageManager.PERMISSION_GRANTED;
}
//Permission function starts from here
private void RequestMultiplePermission() {
// Creating String Array with Permissions.
ActivityCompat.requestPermissions(this, new String[]
{
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_FINE_LOCATION
}, RequestPermissionCode);
}
// Calling override method.
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (grantResults.length > 0) {
boolean ReadPhoneStatePermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean AccesFineLocationPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (ReadPhoneStatePermission && AccesFineLocationPermission) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_LONG).show();
}
}
}
知道为什么会发生这种情况以及如何解决它?