Runtime Permissions not working

2k views Asked by At

This is my code in OnCreate() method :

if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.RECIEVE_SMS)) {

} else {

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},1);
}

And in my onRequestPermissionsResult() method :

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

              init();
}else{
         finish();
 }

And my Manifest file has the following permissions

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>

But i'm not getting the dialog asking for the permission and i can't figure out the problem?

2

There are 2 answers

1
AskNilesh On BEST ANSWER

if your app targeting Android 6.0 and above than add runtime permission

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app

add runtime permission using below code for READ_SMS

String permission = Manifest.permission.READ_SMS;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
    String[] permission_list = new String[1];
    permission_list[0] = permission;
    ActivityCompat.requestPermissions(this, permission_list, 1);
}

and than handle result like this

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 1) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(AccountClass.this,"permission granted", Toast.LENGTH_SHORT).show();  
             // perform your action here

        } else {
            Toast.makeText(AccountClass.this,"permission not granted", Toast.LENGTH_SHORT).show();
        }
    }

}
1
ND1010_ On

Try this:

if (ActivityCompat.checkSelfPermission(AddnewPhotoActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(AddnewPhotoActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL_STORAGE);

            } else {

                isPermissionGranted(true);
            }

isPermissionGranted(boolean); ====> Method call

public void isPermissionGranted(boolean permission) {
        if (permission) {


           //Your Operation


        } else {

            Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
        }
    }

onRequestPermissionsResult

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_READ_EXTERNAL_STORAGE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                isPermissionGranted(true);

            } else

            {
                isPermissionGranted(false);
            }
        }

    }