Reputation: 295
I want to get the permission to read SMS in my App. This is my code:
String permission = Manifest.permission.READ_SMS;
if (ContextCompat.checkSelfPermission(getContext(), permission) != PackageManager.PERMISSION_GRANTED){
permissionList.add(permission);
if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permission)){
requestPermissions(new String[]{permission}), SMS_PERMISSION);
}
}
I didn't get dialog to confirm the permission request. For other permissions (like WRITE_STORAGE
, READ_CONTACTS
) I got this dialog. Do you know how to fix it?
Method onRequestPermissionsResult
gives to me that permission isn't granted. But it works, without the confirmation dialog.
Upvotes: 5
Views: 11871
Reputation: 515
You can use this code:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == ACCESS_SMS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "Permission granted"+checkboxChecked, Toast.LENGTH_SHORT).show();
System.out.println("Checkedbox= "+checkboxChecked);
} else {
Toast.makeText(getApplicationContext(), "Permission denied"+checkboxChecked, Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 0
Reputation: 326
Did you add the read_sms
permission in AndroidManifest.xml
as well ?
Upvotes: 1
Reputation: 147
int GET_MY_PERMISSION = 1;
if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.READ_SMS)){
/* do nothing*/
}
else{
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_SMS},GET_MY_PERMISSION);
}
}
this piece of code works fine. I used it on Nougat(api level:25) I hope it should work for you even! I followed this
Upvotes: 0
Reputation: 295
I need the <uses-permission-sdk-23/>
for confirm dialog.
Upvotes: 2