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?