Google Play services LocationSettingsAPI - LocationSettingsRequest Dialog not Showing

1.1k views Asked by At

I was wondering if any of you could help me. I could really use your expertise.

Currently, I have implemented a location tracking function with help of the google play service. It is able to measure current speed and total distance fairly accurately. I want to have the one-click enable function like google maps has and I have implemented it exactly as the android developer page shows. Currently, I have the following code (it's not all of it, but the whole 'requestLocationUpdates' won't be called before the GPS is turned on, it doesn't seem necessary to show it here:

mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    mLocationRequest = new LocationRequest()
            .setInterval(5000)
            .setFastestInterval(500)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    mGoogleApiClient.connect();

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);
    LocationSettingsRequest request = builder.build();

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, request);

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates locationSettingsStates = result.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can initialize location
                    // requests here.

                    Log.d("EquiMoves", "Success");
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.

                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(
                                activity,
                                1000);


                        Log.d("EquiMoves", "Resolution required");
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    Log.d("EquiMoves", "Fail");
                    break;
            }
        }
    });

And the onActivityResult method:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
    switch (requestCode) {
        case 1000:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    // All required changes were successfully made
                    Log.d("EquiMoves", "Ok");
                    break;
                case Activity.RESULT_CANCELED:
                    // The user was asked to change settings, but chose not to
                    Log.d("EquiMoves", "Canceled");
                    break;
                default:
                    Log.d("EquiMoves", "Default");
                    break;
            }
            break;
    }
}

As you can see there are some Logs in the code. Following these logs, it tells me that it passes through all the functions and even reaches onActivityResult, where it immediately goes to Activity.RESULT_CANCELED (Because the log shows "Canceled".

I can't figure out why I don't get the dialog and I hope any of you can help me.

Thank you in advance and have a great day!

Greetings,

Jasper

0

There are 0 answers