Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is LocationSettingsResult startResolutionForResult not calling onActivityResult?

I've seen this Q&A LocationSettingsRequest dialog - onActivityResult() skipped. It isn't the same issue because everything is being done in an Activity already.

The code used is almost verbatim what is given in the Google Play Services examples.

I have an activity, LocationActivity, that connects to GoogleApiClient for getting the user's location. Once connected I create a LocationSettingsRequest to make sure that location settings are turned on. The activity is implementing ResultCallback<LocationSettingsResult>.

ResultCallback<LocationSettingsResult>.onResult() is called and if result.getStatus().getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED then status.startResolutionForResult(this, REQUEST_CHECK_SETTINGS) is called and the dialog is shown. The problem, no matter what is selected, onActivityResult() is never called.

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "GoogleApiClient connected");
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
        .addLocationRequest(new LocationRequest().setPriority(LocationRequest.PRIORITY_LOW_POWER));

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

    result.setResultCallback(this);
}

.

@Override
public void onResult(LocationSettingsResult result) {
    final Status status = result.getStatus();
    Log.d(TAG, "onResult() called with: " + "result = [" + status.getStatusMessage() + "]");
    switch (status.getStatusCode()) {
        case LocationSettingsStatusCodes.SUCCESS:
            getLocation();
            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(this, REQUEST_CHECK_SETTINGS);
            } catch (IntentSender.SendIntentException e) {
                Log.d(TAG, "", e);
                // Ignore the error.
            }
            break;
        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
            showManualInputDialog();
            break;
    }
}

I never get here:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult() called with: " + "requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
    switch (requestCode) {
        case REQUEST_CODE_RESOLUTION:
            retryConnecting();
            break;
        case REQUEST_CHECK_SETTINGS:
            if (resultCode == Activity.RESULT_OK) {
                getLocation();
            } else {
                showManualInputDialog();
            }
            break;
        default:
            super.onActivityResult(requestCode, resultCode, data);
            break;
    }
}

As an aside. It worked a few times on my S3. From what I can tell it stopped working when I chose to never ask again. But, it hasn't ever worked on an emulator or a Tab 10 and it no longer works on my S3.

like image 905
Jeff Engebretsen Avatar asked Dec 18 '15 18:12

Jeff Engebretsen


2 Answers

If you are running this code in Fragment than don't use startResolutionForResult(). Instead use startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CODE_LOCATION_SETTING, null, 0, 0, 0, null);

and overrider onaActivityResult() in your fragment. Result will be delivered to this method only.

like image 58
Bhaumik Ghodasara Avatar answered Oct 07 '22 17:10

Bhaumik Ghodasara


In my case there was this error: I've used

public abstract class AGoogleDriveBase extends ABase implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
//...
@Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
//...

and this function not called using

try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
        }

because when I used a main activity

public class myAct extends AGoogleDriveBase implements ... {
//...
@Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {

the super method not called (not existed this string):

 super.onActivityResult(requestCode, resultCode, data);
like image 20
Vyacheslav Avatar answered Oct 07 '22 17:10

Vyacheslav