Home » Android » google play services – Android GooglePlayServicesUtil.getErrorDialog() does not show the dialog

google play services – Android GooglePlayServicesUtil.getErrorDialog() does not show the dialog

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to check the availability of Google Play Services APK prior to using it. I have got a
device where the package is out of date (the log reads “…Google Play services out of date. Requires 3225100 but found 3136134“).
The below code shall handle this situation and show a dialog prompting the user to do the update. For a reason unknown to me
the line

GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();

returns immediately showing no dialog (and not blocking the UI thread on an UI event).
Could you please shed a light of what is possible going on and how to rectify the code in order to to get the dialog displayed?

@Override
protected void onResume() {
    super.onResume();

    // Check device for Play Services APK. If check succeeds, proceed with
    //  GCM registration.
    if (checkPlayServices()) {
        gcm = GoogleCloudMessaging.getInstance(this);
        regid = getRegistrationId(context);

        if (regid == null || regid.length() == 0) {
            registerInBackground();
        } else {
            this.user.setGCMRegistrationId(regid);
        }
    } else {
        Log.i(TAG, "No valid Google Play Services APK found.");
    }       
}

/**
 * Check the device to make sure it has the Google Play Services APK. If
 * it doesn't, display a dialog that allows users to download the APK from
 * the Google Play Store or enable it in the device's system settings.
 */
private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        } else {
            Log.i(TAG, "This device is not supported.");
            finish();
        }
        return false;
    }
    return true;
}    


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  switch (requestCode) {
    case PLAY_SERVICES_RESOLUTION_REQUEST:
      if (resultCode == RESULT_CANCELED) {
        Toast.makeText(this, "Google Play Services must be installed.",
            Toast.LENGTH_SHORT).show();
        finish();
      }
      return;
  }
  super.onActivityResult(requestCode, resultCode, data);
}    
How to&Answers:

I encountered some bizarre behavior with code similar to yours when I was including the Google Play Services Library improperly in my project. You have to import as a project the google-play-services_lib directory and you have to include google-play-services.jar in your classpath. The reason is that the project contains a bunch of resources including the ones necessary for displaying the appropriate dialog for getErrorDialog().