Skip to content Skip to sidebar Skip to footer

Handling Cancellation Of MakeGooglePlayServicesAvailable

How do you correctly close an activity when the user declines to update Google play services? I am using makeGooglePlayServicesAvailable() because it seems convenient, but I have n

Solution 1:

I assume that you are pressing "back" to cancel the GPS update request. The error is coming on the call to the super of onDestroy(). That seems to indicate that Android has already anticipated not being able to proceed and has already shut things down. (That is just a guess.)

Anyway, I could not determine a graceful way to close things out with the failure listener callback, but here is a slightly different approach that still uses GoogleApiAvailability. I have tested this out and it seems to work.

Replace your checkGooglePlayServices() with the following:

private static final int GPS_REQUEST_CODE = 1; // arbitrary code

void checkGooglePlayServices() {
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int status = api.isGooglePlayServicesAvailable(this);
    if (status != ConnectionResult.SUCCESS) {
        if (api.isUserResolvableError(status)) {
            // onActivityResult() will be called with GPS_REQUEST_CODE
            Dialog dialog = api.getErrorDialog(this, status, GPS_REQUEST_CODE,
                    new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            // GPS update was cancelled.
                            // Do toast or dialog (probably better to do) here.
                            finish();
                        }
                    });
            dialog.show();
        } else {
            // unrecoverable error
        }
    }
}

This code avoids the callbacks but will still enable you to check for the availability of GPS. Here is the documentation.

Here is the same approach in an example app.

If you want to proceed with the code you have you can do the following hack, but I do prefer the above to capture the crash and hide the nastiness from the user.

@Override
public void onDestroy() {
    try {
        super.onDestroy();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }
}

Solution 2:

Found this snippet from https://www.codota.com/code/java/methods/com.google.android.gms.common.GoogleApiAvailability/makeGooglePlayServicesAvailable

public static void checkPlayServices(Activity activity) {
  final GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
  int errorCode = apiAvailability.isGooglePlayServicesAvailable(activity);
  if (errorCode != ConnectionResult.SUCCESS) {
    if (apiAvailability.isUserResolvableError(errorCode)) {
      apiAvailability.makeGooglePlayServicesAvailable(activity);
    } else {
      Toast.makeText(activity, R.string.push_error_device_not_compatible, Toast.LENGTH_SHORT).show();
    }
  }
}

Post a Comment for "Handling Cancellation Of MakeGooglePlayServicesAvailable"