Handling Cancellation Of Makegoogleplayservicesavailable
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:
privatestaticfinalintGPS_REQUEST_CODE=1; // arbitrary codevoidcheckGooglePlayServices() {
GoogleApiAvailabilityapi= GoogleApiAvailability.getInstance();
intstatus= api.isGooglePlayServicesAvailable(this);
if (status != ConnectionResult.SUCCESS) {
if (api.isUserResolvableError(status)) {
// onActivityResult() will be called with GPS_REQUEST_CODEDialogdialog= api.getErrorDialog(this, status, GPS_REQUEST_CODE,
newDialogInterface.OnCancelListener() {
@OverridepublicvoidonCancel(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.
@OverridepublicvoidonDestroy() {
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
publicstaticvoidcheckPlayServices(Activity activity) {
finalGoogleApiAvailabilityapiAvailability= GoogleApiAvailability.getInstance();
interrorCode= 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"