I implemented an app that uses GPS location and so I am dealing with the permissions. The app runs correctly, all locations are correctly performed. When I install the application, the first fragment opens correctly and shows to user the request for GPS permission. Behind that, I added also a dialog that requests to set ON the GPS in order to start the application. If before first start (after installation) the GPS is set to OFF, when the app starts, it is shown the first fragment on background, the request for GPS permission and then the request to set ON the GPS inside settings. If before first start (after installation) the GPS is set to ON, when the app starts, it is shown the first fragment on background, the request for GPS permission and app crashes maintaining the request for GPS permission active. If I press OK, I restart the application and all works properly. Why this issue when GPS on settings is set to ON? Here below a part of code:
On first fragment, onCreateView I wrote the following code for GPS permission:
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addOnConnectionFailedListener(this)
.addConnectionCallbacks(this)
.build();
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
showAlert(getString(R.string.PermessoDisp), getString(R.string.PermessoDispTitle));
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_REQUEST_LOCATION);
}
} else {
}
In the onStart() I have:
@Override
public void onStart() {
super.onStart();
if (isLocationEnabled()) {
mGoogleApiClient.connect();
} else {
showAlertGPSSettings();
}
}
where showAlertGPSSettings() is the method that ask user to set the GPS ON in order to use app. And I have also:
@Override
public void onConnected(@Nullable Bundle bundle) {
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(500);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_REQUEST_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
}
}
}
When the app crashes (at first start when GPS is set to ON), I have the following error:
java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
The row where the error is located, is inside the onConnected method:
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
Any suggestion?