9

I'm using Google's SettingsClient on an app. It works fine, but there are some users that are not able to use GPS, even if they have location turned on and they had already granted access to location permission. For what I understand, if Settings Client returns RESOLUTION_REQUIRED on it's FailureListener, it's because user has location disabled.

Is that true? or is there any other reason RESOLUTION_REQUIRED is returned?

When RESOLUTION_REQUIRED is returned, GPS icon is not shown on status bar, but it should be there!

This is my code:

    SettingsClient mSettingsClient =
            LocationServices.getSettingsClient(context);

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL_MS);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL_MS);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationSettingsRequest.Builder builder = new 
    LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    mLocationSettingsRequest = builder.build();

    mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
            .addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
                @Override
                public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
                    connected = true;
                    // Fused Location code...
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    connected = false;
                    gpsFailureMessage = e.getMessage();
                }
            });
  • Looks like some of the requirements/permission for location retrieval is not met and based on their documentation , you can ask for permissions for this special code. refer to the documentation and code in https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient – Jimmy Mar 02 '18 at 17:23
  • Check your play services version and your permissions, either of those can cause this to happen – Kushan Mar 02 '18 at 18:16
  • I'm 99% sure that all permissions are granted. I'll ask them to send me their Google Play Services version. Thanks to both of you :) – Juan Carlos Durini Mar 02 '18 at 18:25
  • 1
    facing the same problem. Have you solved the problem? – Saurabh Padwekar Jul 15 '18 at 11:43
  • Hey @SaurabhPadwekar, I haven't been able to solve this problem. I guess it could be something related with low GPS signal, sometimes it works, sometimes it doesn't... I – Juan Carlos Durini Jul 15 '18 at 17:54
  • got any solution for this issue, i am at same situation ? – Omar Dhanish Apr 08 '19 at 06:21
  • I stopped working on this project. What I think is that it depends of the phone's model and company, but I couldn't find a solution :( – Juan Carlos Durini Apr 09 '19 at 15:19
  • I have noticed that, when I use the Fake GPS application for testing purposes that time this type of scenario is generated often.(After testing 2-3 times with multiple different locations). – Dhaval Shah Oct 23 '20 at 13:53

2 Answers2

3

I'm facing this problem and find out this problem cause by Location mode, even though GPS turn on but if Location mode still select "GPS only" will make this exception occur, when i change to another mode, it work fine. Unfortunately you can't set the location mode programmatically but you can send the user directly to the settings screen where he can do that, follow this anwser https://stackoverflow.com/a/25745124/8921450

GPS mode picture

manhtuan21
  • 2,388
  • 2
  • 10
  • 24
0

One way of solving this problem programmatically is by checking the accuracy of the location available as described in this official android documentation. I do it this way, create a companion object (static function) in your locationUtils class like this

class LocationUtils {

    //other methods

   companion object {

    fun  checkLocationRequest(context: FragmentActivity, successCase : () -> Unit) {
        val builder = LocationSettingsRequest.Builder()
            .addLocationRequest(createLocationRequest())

        val client: SettingsClient = LocationServices.getSettingsClient(context)
        val task = client.checkLocationSettings(
            builder.build()
        )

        task.addOnSuccessListener {
            successCase()
        }

        task.addOnFailureListener { exception ->
            if(exception is ResolvableApiException) {
                try {
                    exception.startResolutionForResult(context, CHECK_LOCATION_SETTING)
                } catch (sendEx: IntentSender.SendIntentException) {

                }
            }
        }
    }

you can then call the function like this

LocationUtils.checkLocationRequest(activity){
    runHappyCase() //perform operation you intend to do with the location
}

if the location is not in high precision state, a dialogue prompting the user to choose high precision is shown. you can check the response of the user through onActivityResult()

ilatyphi95
  • 555
  • 5
  • 22