Android marshmallow request permission?

I am currently working on an application that requires several "dangerous" permissions. So I tried adding "ask for permission" as required in Android Marshmallow(API Level 23), but couldn't find how to do it.

How can I ask for permission using new permission model in my app?


  • Request runtime permissions from the docs has code examples you can use.

    The code snippets in this answer are taken from that document.

    Open a Dialog using the code below:

                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},

    Get the Activity result as below:

    @Override public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
              // If request is cancelled, the result arrays are empty.
              if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.          
                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
            // other 'case' lines to check for other
            // permissions this app might request
        } }

    More info: