Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActivityCompat.requestPermissions does not show prompt

I'm attempting to request ACCESS_FINE_LOCATION permissions in order to get the user's current location.

My logging indicates that my app does not currently have this permission when querying ContextCompat.checkSelfPermission(), but when calling ActivityCompat.requestPermissions() nothing is displayed.

My Google map code (implementing OnMapReadyCallback and ActivityCompat.OnRequestPermissionsResultCallback()) is in a FragmentActivity.

I have managed to get the requestPermissions() function working successfully in other Activities in the app, it's just the one with the Google map. It doesn't work when placed in the onCreate() method of the Activity, or in onMapReady() (where it needs to go).

if(ContextCompat.checkSelfPermission(LocationActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Log.d(TAG, "not granted");
        final String[] permissions = new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION};
    if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
            Log.d(TAG, "rationale");
            // Explain to the user why permission is required, then request again
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("We need permissions")
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            ActivityCompat.requestPermissions(LocationActivity.this, permissions, 1);
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

    } else {
        Log.d(TAG, "request" + android.Manifest.permission.ACCESS_FINE_LOCATION);
        // If permission has not been denied before, request the permission
        ActivityCompat.requestPermissions(LocationActivity.this, permissions, 1);
    }
} else {
    Log.d(TAG, "granted");
}

Any ideas? Is it something to do with my Activity's class (FragmentActivity), or possible the Google map calling the permissions request asynchronously?

like image 712
Robin Avatar asked May 10 '16 13:05

Robin


2 Answers

After stripping out my class completely, and it still not working, I realised that this Activity is being instantiated using a TabHost.

When I stop using the TabHost, the prompt is displayed successfully. I guess TabHosts are not supported by the new permissions prompts - is this a bug?

Same problem as App requests aren't showing up

I ended up creating a PermissionsRequestActivity which handles the permission request and response on behalf of my TabHost, then exits (pass the requested permission information in through the Intent extras Bundle).
It passes back the response to the request as a Broadcast, which is picked up by my TabHost.

Bit of a hack but works OK!

like image 115
Robin Avatar answered Oct 27 '22 01:10

Robin


Check that you have already added the requested permission in Android's manifest file like before Android M, only then you will get expected behaviour.

Add the permission to your manifest so you can request it via ActivityCompat.requestPermissions:

<uses-permission android:name="android.permission. ACCESS_FINE_LOCATION" />
like image 38
Alejandro Casanova Avatar answered Oct 27 '22 02:10

Alejandro Casanova