1

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

5
  • You don't need to ask for permissions to show a dialog. Did your app crashed when trying to show a dialog? Commented Oct 22, 2016 at 4:58
  • What permission you are trying to ask?? Commented Oct 22, 2016 at 5:07
  • @SrikarReddy Sorry about my sort description of problem, but i need permission to read from user's sd card to upload attachment with note. Commented Oct 22, 2016 at 5:07
  • you can add permission checking in upload file onClickListener Commented Oct 22, 2016 at 5:12
  • @Basi, yep, i can add permission in onClickListener. but i can't get result of permission request in listener. Commented Oct 22, 2016 at 5:17

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

what about getting result ? How to get result back in our method?
Once the request has been accepted by the user you can read data from the SD card using FileInputStream. I hope this helps stackoverflow.com/a/10550036/4402462
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.