11


我正在尝试在使用之前检查Google Play Services APK的可用性。我有一个设备包已过期(日志显示“ ...Google Play services out of date. Requires 3225100 but found 3136134 ”)。
下面的代码将处理这种情况并显示一个对话框,提示用户进行更新。由于我不知道的原因,这条线

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

立即返回,不显示任何对话框(并且不阻塞 UI 事件上的 UI 线程)。
您能否说明可能发生的情况以及如何纠正代码以显示对话框?

@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);
}    
4

1 回答 1

0

当我在我的项目中不正确地包含 Google Play 服务库时,我遇到了一些与您的代码类似的奇怪行为。您必须将目录作为项目导入,并且必须在类路径中包含 google-play-services.jar。原因是该项目包含大量资源,包括为.google-play-services_libgetErrorDialog()

于 2013-10-30T20:48:40.400 回答