0

So I have problem to the the LocationSettingsRequest dialog to show when GPS is not turned on.

I have been following the SettingsApi Documentation

Here is the code:

enableGpsSetting:

public void enableGpsSetting(){
    if (mGoogleApiClient == null) {

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

        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(30 * 1000);
        locationRequest.setFastestInterval(5 * 1000);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest);

        builder.setNeedBle(true);
        //**************************
        builder.setAlwaysShow(true); //this is the key ingredient
        //**************************

        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                //final LocationSettingsStates = result.getLocationSettingsStates();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        // All location settings are satisfied. The client can initialize location
                        // requests here.
                        //...
                        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(
                                    MainActivity.this,
                                    REQUEST_CHECK_SETTINGS);
                        } 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.
                        //...
                        break;
                }
            }
        });
    }
}

onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)   {
    switch (requestCode) {
 // Check for the integer request code originally supplied to   startResolutionForResult().
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    //startLocationUpdates();
                    break;
                case Activity.RESULT_CANCELED:
                    enableGpsSetting();
                    break;
            }
            break;
    }
}

The changes I have made is Outerclass.this to MainActivity.this I am extending FragmentActivity dont know if it make any differece.

Its a bit strange when the code is as the guidlines and dont work.

Pang
  • 9,564
  • 146
  • 81
  • 122
codemoonger
  • 613
  • 3
  • 11
  • 22
  • You may have skipped an implementation, try checking this [previous SO ticket](http://stackoverflow.com/questions/31235564/locationsettingsrequest-dialog-onactivityresult-skipped) that show how to properly implement locationsettingsrequest.builder. I hope this help. – Mr.Rebot May 02 '16 at 22:36
  • For someone still experiencing this issue, you need to start the location settings request after you have connected to Google Play Services which is `GoogleApiClient.ConnectionsCallback#onConnected` – mr5 Mar 28 '17 at 09:08

2 Answers2

0

This problem happened to me once. (Actually today!)

I was testing something else, and at a certain point in my application is made to call this dialog to change the GPS settings. After testing several times, I was already tired of all time deny permission gps. (GPS was not important to what I was testing.) So I used the never button. And the dialog never appeared. Then when I needed him again he did not appear.

On the line

final Status status = result.getStatus()

He began to return the code 8502 - and into the switch case SETTINGS_CHANGE_UNAVAILABLE.

case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:

If this is the case you can uninstall your application and re-install, the dialog change configuration reappears.

Lovera
  • 172
  • 2
  • 15
0

Use the following code.

protected void createLocationRequest() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000*5);
    mLocationRequest.setFastestInterval(1000*2);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

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

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

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {

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

                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(
                                CurrentActivity.this,
                                REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:

                    break;
            }
        }
    });
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Manish Malviya
  • 546
  • 2
  • 15