0

In my application i want to perform specific function that exist in non activity class but before perform this function should check for some permission is granted or not. I handle request permission in run time but how i can check if the user deny this permission and check never show again.

In my case i check if the permission is granted using

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

but this give me false any time i check it also when set permission Always deny from settings and when try to check it using another way

if (ActivityCompat.shouldShowRequestPermissionRationale(context, Manifest.permission.READ_CONTACTS))

but also give me false when deny permission and check never show again.

so what is the best way to check if user deny any permission and check never show again from run time permission dialog or deny it from application settings

Azak
  • 270
  • 1
  • 5
  • 15

1 Answers1

1

here is the code for checking permission particularly.

 private void requestP() {

// Write all permissions in PERMISSIONS array

    String[] PERMISSIONS = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    if (!hasPermissions(this, PERMISSIONS)) {
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }

}

public static boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {

        case PERMISSION_ALL: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0) {

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

                    // Read Phone State Permission allowed
                } else {
                   // Read Phone State Permission denied 
                }

                if (grantResults[1] == PackageManager.PERMISSION_GRANTED) {

                   // Write External Storage Permission allowed
                } else {
                   // Write External Storage Permission denied
                }


            } else {

                // All Permissions denied
            }
            // runAfterPermissionGranted

            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

You can store flags for all permissions in onRequestPermissionResult() and check that flag whenever you want.

kwaghela
  • 126
  • 7