5

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.

Floern
  • 33,559
  • 24
  • 104
  • 119
Vanya Sakharovskiy
  • 295
  • 1
  • 3
  • 16

4 Answers4

2

I need the <uses-permission-sdk-23/> for confirm dialog.

Vanya Sakharovskiy
  • 295
  • 1
  • 3
  • 16
1

Did you add the read_sms permission in AndroidManifest.xml as well ?

Nuri Tasdemir
  • 9,720
  • 3
  • 42
  • 67
Udit Khandelwal
  • 326
  • 3
  • 15
0
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

Anuroop Pendela
  • 147
  • 1
  • 8
0

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();
        }
    }
}
Matt Ke
  • 3,599
  • 12
  • 30
  • 49