Firstly, I know this is a duplicate question, but I've been over the answers from other similar questions and haven't been able to find success with any of those solutions.
I've developed an app which works perfectly on my testing device which is a Samsung S4 running Android L (5.0.1) however I would like for this app to also work on newer versions of Android.
I understand that requesting permissions have changed for Android M+ so that they have to be asked at run-time, but when I try to implement this the dialog box never appears and hence the required permissions are never asked for.
The permission I am requesting is ACCESS_FINE_LOCATION
.
Here is the relevant code from my manifest file:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Where I ask for permission if it is not already granted:
if (ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
startGeofenceMonitoring();
} else {
askForPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,LOCATION);
}
askForPermission function (slightly modified from Google's example):
private void askForPermission(String permission, Integer requestCode) {
if (ContextCompat.checkSelfPermission(MapsActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, permission)) {
//This is called if user has denied the permission before
//In this case I am just asking the permission again
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);
} else {
Log.d(TAG, "askForPermission: " + permission);
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);
}
} else {
Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
}
}
And the onRequestPermissionsResult function:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.d(TAG, "onRequestPermissionsResult: " + requestCode);
switch (requestCode) {
case 1:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "onRequestPermissionsResult: request");
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
startGeofenceMonitoring();
}
break;
}
}
As I said, the dialog box which is asking for the permissions never appears, but my console tells me that ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);
is called, and after a lot of research I'm still not really sure why it isn't working. The only thing that I think it could be due to is that this Activity is a Fragment activity displaying a Google Maps map. I did try to request the permissions in another activity but the exact same thing happens.
Thanks.
EDIT
So I just tested the code I posted here in a completely fresh project and it ran just fine and showed the dialog box. I am now really confused as to what is preventing this dialog box from being shown in my current project.