我在项目中实现了运行时权限,主要是READ_PHONE_STATE,READ_CONTACTS,GET_ACCOUNTS。假设用户拒绝3个中的2个,那么我必须显示关于每个被拒绝权限的单个对话框消息。我该怎么做
代码: -
PermissionManager.with(this)
.key(2100)
.permission(PermissionEnum.READ_PHONE_STATE, PermissionEnum.READ_CONTACTS, PermissionEnum.GET_ACCOUNTS, PermissionEnum.READ_SMS, PermissionEnum.ACCESS_FINE_LOCATION, PermissionEnum.ACCESS_COARSE_LOCATION, PermissionEnum.WRITE_EXTERNAL_STORAGE, PermissionEnum.READ_EXTERNAL_STORAGE)
.askAgain(true)
.askAgainCallback(new AskAgainCallback() {
/*Callback to handle denied permission response*/
@Override
public void showRequestPermission(UserResponse response) {
showDialog(response);
}
})
.callback(new SmartCallback() {
/* To handle the result of permissions after all successful/ unsuccessful user transactions*/
@Override
public void result(boolean allPermissionsGranted, boolean somePermissionsDeniedForever) {
if (allPermissionsGranted) {
init();
} else {
finish();
}
}
})
.ask();
显示对话框: -
private void showDialog(final AskAgainCallback.UserResponse response) {
new AlertDialog.Builder(Splash.this)
.setTitle("Permission needed")
.setMessage("This app really need to use these permissions, do you want to authorize it?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
response.result(true);
}
})
.setNegativeButton("NOT NOW", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
response.result(false);
finish();
}
})
.setCancelable(false)
.show();
}
但需要在对话框中为每个被拒绝的权限显示单个消息。
答案 0 :(得分:0)
final String[] permisions = {Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION};
ActivityCompat.requestPermissions(this, permisions, Permissions_Request_Code);
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) { // RequestCode for our Permissions
case Permissions_Request_Code:
if (grantResults.length != 0) { // Check Granted Results Not Null
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission Granted for Manifest.permission.ACCESS_FINE_LOCATION
// DO What You Want
} else// Permission Denied for Manifest.permission.ACCESS_FINE_LOCATION
{
// Display Error Dialog
}
if (grantResults[1] == PackageManager.PERMISSION_GRANTED) {// Permission Granted for Manifest.permission.ACCESS_COARSE_LOCATION
// DO What You Want
} else // Permission Denied for Manifest.permission.ACCESS_COARSE_LOCATION
{
// Display Error Dialog
}
}
break;
}
}