Reputation: 169
I have an app where I want to show the user a location by dropping a pin when they open an activity "Map". I also want to display the users location, and if something goes wrong with the marker from the information provided from the extras, the map centers on the users location.
As many others I one day noticed that my app wasn't showing the user's location above Android 6.0. So I set out and search SO and Google and found a few threads and tutorials. So turns out I have to check if permissions are already available or if they need to be granted by calling the method checkSelfPermissions, and if they haven't been granted I have to request them by calling requestPermissions. When they have been requested I want to set the marker and show the users location (or center the map if something is messed up with the marker info from the extras). I want to do this with my method setUpMarker. Below is my onMapReady (called when the map is ready) and onRequestPermissionsResult (called when there is a user response from the permission request) methods:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Permission is not yet available and needs to be asked for
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
// We provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
new AlertDialog.Builder(Map.this)
.setMessage("To show the location the permission is needed")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(Map.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_PERMISSION_REQUEST_ID);
}
})
.setNegativeButton("Cancel", null)
.create()
.show();
} else {
// Permission has not been granted yet. Request it directly.
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_PERMISSION_REQUEST_ID);
}
} else {
// Permission is already available
setUpMarker();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_ID: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// The permission request was granted, we set up the marker
setUpMarker();
} else {
// The permission request was denied, we make the user aware of why the location is not shown
Toast.makeText(this,"Since the permission wasn't granted we can't show the location",Toast.LENGTH_LONG).show();
}
return;
}
}
}
Now, I still get an error in my setUpMarker method when I want to enable the users location and when I want to center the map on the users location. The error I get is this:
Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with 'checkPermission') or explicitly handle a potential 'SecurityException'.
But I've already checked for the permissions! Here is my setUpMarker:
private void setUpMarker() {
//HERE I GET THE ERROR
mMap.setMyLocationEnabled(true);
if(!getIntent().getExtras().isEmpty()) {
// Latitude and longitude was retrieved successfully, set up marker
...
} else {
// Latitude and longitude was not retrieved, zoom to current location
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
// AND HERE I GET THE ERROR TOO
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude()))
.zoom(17)
.bearing(90)
.tilt(40)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
}
How do I get the application to remember the permissions, or get around this in another way? I've checked out similar threads but none seem to have this kind of problem.
Upvotes: 0
Views: 1611
Reputation: 199805
This is a Lint error - you can either add the same ContextCompat.checkSelfPermission
call to the method (even though you know it'll always return granted) to please Lint or suppress the Lint error.
Upvotes: 1