Intimate
Intimate

Reputation: 456

Requesting Runtime Permission from Custom Dialog in Android

I am developing an application which shows a custom listview of notes and a Button for adding a new note. When user clicks on button, app shows a full screen dialog to add note with some additional detail. User can also upload an attachment with note as clicking on attachment button. but the problem is after Android M(API 23) this task requires runtime permission.

According to Google Developers, result of permission request will be delivered in onRequestPermissionsResult() method. I don't know how can I get this result in method which I used to show fullscreen dialog.

This is how my method looks like:

private void showCreateNoteDialog() {

    //create dialog body...
    final Dialog createDialog = new Dialog(NotesActivity.this,R.style.MyFullscreenDialog);
    createDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    LayoutInflater inflater = this.getLayoutInflater();
    createDialog.setContentView(inflater.inflate(R.layout.customdialog_createNote, null));
    createDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);}

Edit :

The permission require for uploading a file is Read from Sd card or External storage

Upvotes: 1

Views: 3314

Answers (3)

Atiq
Atiq

Reputation: 14835

Well the integration of Run time permission can be a lot easier with some useful libraries, my favorite one is PermissionUtils.

All you need to do is compile the dependency in app level build.gradle

dependencies {
    compile 'rebus:permission-utils:1.0.3'
}

then in onCreate of your activity

PermissionManager.with(YourActivity.this)
    .permission(PermissionEnum.READ_PHONE_STATE, PermissionEnum.READ_EXTERNAL_STORAGE) // You can put all permissions here
    .askagain(true)
    .ask();

and that's it. You can find more information here.

Upvotes: 2

Drez
Drez

Reputation: 498

You cannot get a result straight in you showCreateNoteDialog method, but this is what you can do (pseudo code):

showCreateNoteDialog() {
    /* some code goes here */
    if (checkPermission) {
        newMethodCalledFromNoteDialog();
    } else {
        requestPermission();
       // return?
    }
}

newMethodCalledFromNoteDialog() {
        // part of code which needs permission
        // some code depending on previous code
}

onRequestPermissionsResult() {
    if (permissionGranted) {
        newMethodCalledFromNoteDialog();
    }
}

Upvotes: 0

Srikar Reddy
Srikar Reddy

Reputation: 3708

Try something like this.

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

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_EXTERNAL_STORAGE)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

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

        // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

This is how you implement Runtime permission. For more info check the official developer site. Also, take a look at Normal and Dangerous Permissions to know more about which permissions need to be asked at runtime.

Upvotes: 0

Related Questions