I want to ask for the SEND_SMS and READ_SMS runtime permissions, if they are not given. This is my code:
private ActivityResultLauncher<String[]> mRequestPermissionsLauncher;
String[] PERMISSIONS = {
Manifest.permission.SEND_SMS,
Manifest.permission.READ_SMS
};
@Override
protected void onCreate(Bundle savedInstanceState) {
mRequestPermissionsLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), result -> {
Log.d("DEBUG",result.toString());
});
if (!hasPermissions(this, PERMISSIONS)) {
if(shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS) || shouldShowRequestPermissionRationale(Manifest.permission.READ_SMS)){
new AlertDialog.Builder(this)
.setTitle("Permission Needed")
.setMessage("Please press OK to allow the permissions")
.setCancelable(false)
.setPositiveButton("OK", (dialog, which) -> mRequestPermissionsLauncher.launch(PERMISSIONS)).create().show();
}
else{
mRequestPermissionsLauncher.launch(PERMISSIONS);
}
}
}
public static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
After I allow the permissions and debug the result, it keeps showing:
{android.permission.SEND_SMS=true, android.permission.READ_SMS=false}
Why is my READ_SMS permission false?
EDIT: Never mind, the issue is resolved. I forgot to declare the READ_SMS permission in the manifest file. Silly me :p
You can't and that's by design - to prevent abuse.
You're better of following the official guidelines.
The only way to request permissions again, is to navigate the user to app info, where the user has to manually allow it:
You're also using a deprecated way of requesting permissions. Consider using
ActivityResultContracts.RequestMultiplePermissions():