简体   繁体   中英

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

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 .

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.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM