0

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?

  • "and app crashes" -- use Logcat to examine the stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this If you do not understand what the stack trace is telling you, edit your question and provide the complete stack trace. – CommonsWare Jun 13 '20 at 16:03
  • Thanks CommonsWare, If you see on my text above I wrote at the end the error I got – Mauro Marchiori Jun 13 '20 at 16:40
  • Sorry, I was looking for a full stack trace (and I recommend that you always post one when you have a crash). Your permission logic is a bit broken. You check for permissions... but you still create the `GoogleApiClient`. You then call `connect()` on that `GoogleApiClient` even if you do not have permission, and in `onConnected()` you try using locations even if you do not have permission. Change your `GoogleApiClient` logic such that you only create and connect to it if you have permission, either because you started with permission or you were called with `onRequestPermissionsResult()`. – CommonsWare Jun 13 '20 at 16:45

1 Answers1

0

Did you add permission in your manifest?

android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.

This permissions are needed.

Felipe Palma
  • 378
  • 1
  • 9