AndroidExterminator
AndroidExterminator

Reputation: 179

Android Permission Request: onRequestPermissionsResult is never called / reached - Possible Context Issue

Request Permissions:

public class GooglePermissions extends ExternalClass
{
    //...
    private void checkPermissions()
    {
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions( (Activity) mContext, new String[]{Manifest.permission.GET_ACCOUNTS}, REQUEST_GET_ACCOUNTS);
        }
    }
    //...
}

Permission Request Result:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
{
    switch (requestCode)
    {
        case REQUEST_GET_ACCOUNTS:
        {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                Log.d(TAG, "Permissions Granted");
            }
            else
            {
                Log.d(TAG, "Permissions Denied");
            }
            return;
        }
    }
}

I'm attempting to perform an Android permission check - however the onRequestPermissionsResult method is never called.

I suspect the culprit is: (Activity) mContext

...however I cannot simply use: 'this' because the class this code is contained is not an Activity.

Suggestions are appreciated.

Upvotes: 0

Views: 502

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

onRequestPermissionsResult() needs to be implemented on the Activity that you pass into requestPermissions().

Suggestions are appreciated

Request your permissions in your activity or fragment. Those permission requests have to be tied to your UI anyway, so they make sense to the user (e.g., they tapped on such-and-so action bar item, which triggered the permission request).

If you wish to forward the results of the permission request to some other object, you are welcome to do so.

Upvotes: 2

Related Questions