Reputation: 51
In my non-Activity class i requested for a Fine location permission but the call back onRequestPermissionsResult "never gets called" .now i have seen some questions related to this but they all considered that request is being made from an Activity or the fragment, non of them considered making a request from a non activity class. here is my code
public class Initial implements
ActivityCompat.OnRequestPermissionsResultCallback{
and this is implementation for onRequestPermissionResultCallback method
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
Log.i("flow", "onRequestPermissionsResult: ");
switch (requestCode) {
case 101: {
// If request is cancelled, the result arrays are empty.
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
} else {
permissionDeniedsnackBar();
Toast.makeText(context,"permission denied",Toast.LENGTH_SHORT).show();
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
//add more cases if you are making more than one permission request
}
}
also my request for permission code
private void requestPermission() {
ActivityCompat.requestPermissions((Activity) context, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}
, 101);
}
here is my constructor
public Initial(Activity activity) {
Log.i("flow", "Initial: ");
mActivity = activity;
context = (Context) activity;
}
i have search through this site and could find a solution to this specific question
Solved : as some folks mentioned onRequestPermissionsResult method of the "activity" that i passed in as a parameter to ActivityCompat.requestPermissions(activity,..) method while making a permission request will be called. so i managed this onRequestPermissionsResult in that Activity itself and it worked. thanks
Upvotes: 2
Views: 1919
Reputation: 881
You are passing the context of Activity while asking for permission, that is why onRequestPermissionsResult of activity will be called. Sorry but you will not be able to receive this function callback in the Initial class of yours. However you can transfer the response to this class from the activity using an interface or something.
Upvotes: 4