4

I have an activity in Which I am loading a fragment. I am loading data in this fragment in recyclerview using recyclerviewadapter.

In Recyclerview adapter, on some button click I need the below permission:

READ_EXTERNAL_STORAGE

For this I have requested to grant the permission using below code

public void checkPermission(){

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

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)mContext,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {
            // Show an expanation 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.

            ActivityCompat.requestPermissions((Activity)mContext,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    ConstantVariables.READ_EXTERNAL_STORAGE);
        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions((Activity)mContext,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    ConstantVariables.READ_EXTERNAL_STORAGE);

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

Which in turn call the below method

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case ConstantVariables.READ_EXTERNAL_STORAGE:
            // 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.
            }
            break;

        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

Now how do I notify my adapter that permission is granted and Now I can continue with my task.

Can I return any boolean variable from checkPermission() method which will return true/false if permission is granted or not, so that in adapter I can check this variable and can proceed to my task.

Please help me if anyone have any idea here.

Thanks a lot in advanced.

Prithniraj Nicyone
  • 5,021
  • 13
  • 52
  • 78
  • which kind of task you want to perform in adapter ? – vabhi vab Mar 10 '16 at 12:54
  • In cases where there is a specific actionable item in the List Item View, say "Download", then we might need to check whether the User has given the WRITE_STORAGE permission or not, if not given, then this case comes. – Sri Krishna Dec 30 '16 at 10:30
  • Try this it may be work http://stackoverflow.com/a/41221852/5488468 – Bipin Bharti Jan 03 '17 at 11:07

1 Answers1

0

Your question is not clear enough. But if what you are trying to achieve is to grant runtime permission OnClick a button in your adapter class, then try this:

In your adapter class, create an interface, say

public interface WillReceiveClickFromFragment {
    void receiveClick();
}

Now, still in your adapter class create a variable of the interface type, 'WillReceiveClickFromFragment' and pass it to your adapter class constructor

private WillReceiveClickFromFragment willReceiveClickFromFragment;

public YourAdapterClassConstructor(..., WillReceiveClickFromFragment willReceiveClickFromFragment){
    this.willReceiveClickFromFragment = willReceiveClickFromFragment;
}

Finally, in the button inside your adapter class where you perform the click operation, put this in your OnClickListener

yourButton.setOnClickListener(v -> {
    WillReceiveClickFromFragment.receiveClick();
});

Now in your Fragment class, implement the interface you just created in the adapter class and override the interface method

public class YourFragmentClassName extends Fragment implements YourAdapterClassName.WillReceiveClickFromFragment {

                  ...

    @Override
    public void receiveClick(){
        /*call your check permisson method here*/
        checkPermission();
    }
}