Android中 GooglePlayServicesUtil.getErrorDialog()方法不显示对话框

11
我正在尝试在使用Google Play服务之前检查其可用性。我有一个设备,其中该软件包已过时(日志显示“...Google Play服务已过时。需要3225100但发现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);
}    
1个回答

0
当我在我的项目中不正确地包含Google Play服务库时,我遇到了一些类似于你的代码的奇怪行为。你必须将google-play-services_lib目录作为一个项目导入,并将google-play-services.jar包含在你的类路径中。原因是该项目包含一堆资源,包括显示适当对话框所需的资源getErrorDialog()

我做了这两件事情,但 getErrorDialog 仍然没有任何作用。 - theblang
最终按照建议做了,问题已经解决了。感谢提示。 - quirkfly
我仍然看到同样的问题。这个库应该被导出吗?虽然在模拟器中它可以工作。 - Jorge
@Jorge 应该通过包含库项目来在设备上安装库。这样可以保持库的最新状态,至少我是这么认为的。 - mharper
是的,使用 isGooglePlayServicesAvailable(this) 检查库,并根据接收到的 resultCode 更新库、安装它或只是接收一个错误,表示设备不受支持。但在我的情况下,显示的 errorDialog 是空的。 - Jorge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接