-5

Am developing android runtime permissions. I have done all parts. But am frustrated with one thing.

I want to know the specific permission was already denied with never ask again or not.

Surely we can get the result once we calling this below API

 ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);

we can get the result at here

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

    if (requestCode == REQUEST_CAMERA) {
         // here we can get the result
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

But my need is without requesting the requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS); API i want to find that state never ask again is check or not

Please share the things if anybody knows.

Avi K.
  • 1,734
  • 2
  • 18
  • 28
Naveen Kumar Kuppan
  • 1,424
  • 1
  • 10
  • 12

2 Answers2

2

But my need is without requesting the requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS); API i want to find that state never ask again is check or not

That is going to be difficult.

You can call shouldShowRequestPermissionRationale() on ActivityCompat. This will return true if:

  • You have requested this permission from the user previously
  • The user denied that permission
  • The user has not checked the "don't ask again" checkbox

You can call checkSelfPermission() on ContextCompat. This will return true if:

  • You have requested this permission from the user previously
  • The user granted that permission

You can also track yourself whether you have ever requested this permission from the user previously, such as storing that information in a SharedPreferences.

If:

  • You know that you have requested this permission previously (via the SharedPreferences), and
  • checkSelfPermission() returns false (so the user has not granted the permission), and
  • shouldShowRequestPermissionRationale() returns false (so you are not supposed to provide rationale to the user)

then the permission is in the "don't ask again" state.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

I had the same problem and I figured it out. To make life much simpler, I wrote an util class to handle runtime permissions.

public class PermissionUtil {
    /*
    * Check if version is marshmallow and above.
    * Used in deciding to ask runtime permission
    * */
    public static boolean shouldAskPermission() {
        return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M);
    }
private static boolean shouldAskPermission(Context context, String permission){
        if (shouldAskPermission()) {
            int permissionResult = ActivityCompat.checkSelfPermission(context, permission);
            if (permissionResult != PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        }
        return false;
    }
public static void checkPermission(Context context, String permission, PermissionAskListener listener){
/*
        * If permission is not granted
        * */
        if (shouldAskPermission(context, permission)){
/*
            * If permission denied previously
            * */
            if (context.shouldShowRequestPermissionRationale(permission)) {
                listener.onPermissionPreviouslyDenied();
            } else {
                /*
                * Permission denied or first time requested
                * */
if (PreferencesUtil.isFirstTimeAskingPermission(context, permission)) {
                    PreferencesUtil.firstTimeAskingPermission(context, permission, false);
                    listener.onPermissionAsk();
                } else {
                    /*
                    * Handle the feature without permission or ask user to manually allow permission
                    * */
                    listener.onPermissionDisabled();
                }
            }
        } else {
            listener.onPermissionGranted();
        }
    }
/*
    * Callback on various cases on checking permission
    *
    * 1.  Below M, runtime permission not needed. In that case onPermissionGranted() would be called.
    *     If permission is already granted, onPermissionGranted() would be called.
    *
    * 2.  Above M, if the permission is being asked first time onPermissionAsk() would be called.
    *
    * 3.  Above M, if the permission is previously asked but not granted, onPermissionPreviouslyDenied()
    *     would be called.
    *
    * 4.  Above M, if the permission is disabled by device policy or the user checked "Never ask again"
    *     check box on previous request permission, onPermissionDisabled() would be called.
    * */
    public interface PermissionAskListener {
/*
        * Callback to ask permission
        * */
        void onPermissionAsk();
/*
        * Callback on permission denied
        * */
        void onPermissionPreviouslyDenied();
/*
        * Callback on permission "Never show again" checked and denied
        * */
        void onPermissionDisabled();
/*
        * Callback on permission granted
        * */
        void onPermissionGranted();
    }
}

And the PreferenceUtil methods are as follows.

public static void firstTimeAskingPermission(Context context, String permission, boolean isFirstTime){
SharedPreferences sharedPreference = context.getSharedPreferences(PREFS_FILE_NAME, MODE_PRIVATE;
 sharedPreference.edit().putBoolean(permission, isFirstTime).apply();
 }
public static boolean isFirstTimeAskingPermission(Context context, String permission){
return context.getSharedPreferences(PREFS_FILE_NAME, MODE_PRIVATE).getBoolean(permission, true);
}

Now, all you need is to use the method * checkPermission* with proper arguments.

Here is an example,

PermissionUtil.checkPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    new PermissionUtil.PermissionAskListener() {
                        @Override
                        public void onPermissionAsk() {
                            ActivityCompat.requestPermissions(
                                    thisActivity,
              new String[]{Manifest.permission.READ_CONTACTS},
                            REQUEST_EXTERNAL_STORAGE
                            );
                        }
@Override
                        public void onPermissionPreviouslyDenied() {
                       //show a dialog explaining permission and then request permission
                        }
@Override
                        public void onPermissionDisabled() {
Toast.makeText(context, "Permission Disabled.", Toast.LENGTH_SHORT).show();
                        }
@Override
                        public void onPermissionGranted() {
                            readContacts();
                        }
                    });

I want to know the specific permission was already denied with never ask again or not.

If user checked Never ask again, you'll get callback on onPermissionDisabled.

Happy coding :)

muthuraj
  • 1,033
  • 15
  • 22