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.