GooglePlayServicesUtil.getErrorDialog is null

依然范特西╮ 提交于 2019-11-27 19:37:51

Google suggests (also in docs) calling getErrorDialog() if the result code is SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED or SERVICE_DISABLED. So it may be that the last possible status code (SERVICE_INVALID) is what's causing trouble.

I'm using the following code and so far it seems to work ok (testing in emulator, platform 2.3.3):

int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity.getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS) {
    activity.selectMap();
} else if (resultCode == ConnectionResult.SERVICE_MISSING ||
           resultCode == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ||
           resultCode == ConnectionResult.SERVICE_DISABLED) {
    Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, activity, 1);
    dialog.show();
}

Seems like you have to check with isUserRecoverableError before trying to display the dialog.

  int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
  if (status != ConnectionResult.SUCCESS) {
    if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
      GooglePlayServicesUtil.getErrorDialog(status, this, 
      REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
    } else {
      Toast.makeText(this, "This device is not supported.", 
          Toast.LENGTH_LONG).show();
      finish();
    }
  }

Based on Rahim's code, I add the capability of preventing the user to dismiss the Google Play Services dialog (by hitting the back button) and continue using the app without Google Play Services installed:

private void checkGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (status != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, 0);
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    MainActivity.this.finish();
                }
            });
            dialog.show();
        } else {
            Toast.makeText(this, "This device is not supported.", Toast.LENGTH_LONG).show();
            finish();
        }
    }
}
DaveAlden

An update of the answer by @Nevermore, since GooglePlayServicesUtil methods are deprecated in favour of GoogleApiAvailability:

GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(activity.getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS) {
    activity.selectMap();
} else if (resultCode == ConnectionResult.SERVICE_MISSING ||
           resultCode == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ||
           resultCode == ConnectionResult.SERVICE_DISABLED) {
    Dialog dialog = googleApiAvailability.getErrorDialog(activity, resultCode, 1);
    dialog.show();
}

Note the order of the first two parameters in getErrorDialog() has been switched around in the GoogleApiAvailability implementation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!