1

I want to know the best practise to ask user for permission check and the code to run if user declines that particular permission access.

  • > Hey, Your Question's best solution at here : [ Github : Runtime Permission for Android M+ Devices](https://github.com/nilsorathiya/RuntimePermissionForAndroidMPlus) – Narendra Sorathiya Mar 28 '16 at 08:58

6 Answers6

6

This example I copied from http://www.applicoinc.com/blog/android-m-permissions-review/ sets state for the CONTACTS permission:

private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 1;
private static String[] PERMISSIONS_CONTACT =
{Manifest.permission.READ_CONTACTS}   if
(checkSelfPermission(PERMISSIONS_CONTACT)) {    Log.i(TAG,
           "Contact permissions have already been granted. Displaying contact details."); } else {    Log.i(TAG, "Contact permissions has
NOT been granted. Requesting permission.");   
requestPermissions(PERMISSIONS_CONTACT,
PERMISSIONS_REQUEST_READ_CONTACTS); }   …   @Override public void
onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {    switch (requestCode) {
       case PERMISSIONS_REQUEST_READ_CONTACTS: {
           if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               // permission was granted, yay! do the
               // calendar task you need to do.
               Log.d(TAG, "permission granted");
           } else {
               // permission denied, boo! Disable the
               // functionality that depends on this permission.
               Log.d(TAG, "permission denied");
           }
           return;
       }    } }
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37
3

Dexter is an Android library that simplifies the process of requesting permissions at runtime.

Usage of Dexter

Dhaval Jivani
  • 9,467
  • 2
  • 48
  • 42
1

Some hint:

  • Always check for permission before calling operations on Google Play Services because they require permission but the management is in charge of you;
  • Don't ask for granted permissions: when you call request permissions with multiple permissions the system doesn't care if one or two permissions are already granted, the dialog is shown for all! So keep track only of permissions really needed. It could be strange for user grant already granted permissions;
  • Always check if it's needed to show a reason to the user using the method shouldShowRequestPermissionRationale() it could be not really obvious why you are asking a permission;
  • Send a notification to the user if you are checking for permission in background, in a service for example and allow it to grant the permission going with a tap in the app details system activity directly.
greywolf82
  • 21,813
  • 18
  • 54
  • 108
0

Take a look here - there's a flowchart that explains the whole process quite well.

Bottom line is that according to Android's documentation, you should always check and ask for permission if you don't have it (Android will automatically return DENIED in the callback if the user said to never ask again) and you should display a short message if the user has already declined you once in the past but hasn't marked the never ask again option.

Ronnie
  • 72
  • 1
0

For Example Camera Run time permission : This Code you have to put in to camera button click

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            givePerMissons();

        } else {
            openCamera();
        }  
     public void givePerMissons() {
        if ((Utils.checkPermission(MainActivity.this, Manifest.permission.CAMERA)) {
            openCamera();
} else {
            Utils.givePermisson(MainActivity.this, Manifest.permission.CAMERA, Utils.PERMISSOIN);

            if (!Utils.permissionsList.isEmpty()) {
                requestPermissions(Utils.permissionsList.toArray(new String[Utils.permissionsList.size()]), Utils.PERMISSOIN_CODE);
            }
        }
    }
@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case Utils.PERMISSOIN_CODE: {
                if ((Utils.checkPermission(MainActivity.this, Manifest.permission.CAMERA)) {
                    openCamera();
                } else {
                    Toast.makeText(getApplicationContext(),"Write Your message",Toast.LENGTH_LONG).show();
                }
            }
        }
    }

This Code you have to put in your utils Class:

public static final List<String> permissionsList = new ArrayList<String>();

public static void givePermisson(Context context, String permisson, String permissonType) {

        int per = context.checkSelfPermission(permisson);
        if (per != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permisson);

        } else if (per != PackageManager.PERMISSION_DENIED) {
        }
    }

public static boolean checkPermission(Context context, String permission) {

        try {
            PackageManager pm = context.getPackageManager();

            if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
                return true;
            } } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33
-1

if ( ActivityCompat.shouldShowRequestPermissionRationale (this, Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale (this, Manifest.permission.RECORD_AUDIO) ) {

Toast.makeText (this,
                R.string.permissions_needed,
                Toast.LENGTH_LONG).show ();

    } else {

        ActivityCompat.requestPermissions (
                this,
                new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO},
                CAMERA_MIC_PERMISSION_REQUEST_CODE);

    }