I'm using the Google location API to show the users location on the map.
When the app loads for the first time, rationale dialog is displayed explaining why user needs to enable access to location. Then, runtime permission dialog
is shown, and users clicks “Allow” to enable location access. The Map then loads fine without any crashes.
However, when the app resumes (after going into background), no rationale dialog appears since user has already granted location access. In this scenario, only the runtime permission dialog
appears. Now if the user clicks “Deny”, the app crashes. This code works fine in Android versions below M (Lollipop, Jellybean, KitKat etc.)
Is there a way to handle the runtime exception at this stage?
The error is :
java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
I'm using the standard Sample MyLocation demo app without any 3rd party library:
I've also tried using the PermissionsDispatcher Library, but the same errors persist.
PermissionDispatcher Android-Google-Maps-Demo
private void enableMyLocation() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Permission to access the location is missing.
PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
Manifest.permission.ACCESS_FINE_LOCATION, true);
} else if (mMap != null) {
// Access to the location has been granted to the app.
mMap.setMyLocationEnabled(true);
}
}
Any help is highly appreciated.