Vanya Sakharovskiy
Vanya Sakharovskiy

Reputation: 295

Android 6.0 Permissions. Read SMS

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

Answers (4)

Ayan Bhattacharjee
Ayan Bhattacharjee

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

Udit Khandelwal
Udit Khandelwal

Reputation: 326

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

Upvotes: 1

Anuroop Pendela
Anuroop Pendela

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

Vanya Sakharovskiy
Vanya Sakharovskiy

Reputation: 295

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

Upvotes: 2

Related Questions