GooglePlayServicesUtil.getErrorDialog 无效

GooglePlayServicesUtil.getErrorDialog doesn't work

之前有人问过here and here, but there are not useful answers, so I'll try to be specific: I'm trying to implement Google Cloud Messaging on an app developed using Android Studio 1.2.2. I have installed Google Play Services as explained here, and I implemented the checkPlayServices() method as suggested in this sample

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;
}

如果设备没有 Google Play 服务或者如果它已过时,此方法应提醒用户并提供一个按钮以轻松 install/update 它。我在出厂设置为 Android 4.1.2 的 Samsung Galaxy S II 上测试了该应用程序,因此它没有更新的 Google Play 服务。我已经确认 GooglePlayServicesUtil.getErrorDialog() 方法被调用,但对话框没有出现。相反,我在 logcat:

中得到了以下内容
06-14 19:19:45.691  10616-10616/? I/dalvikvm﹕ Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
06-14 19:19:45.691  10616-10616/? W/dalvikvm﹕ VFY: unable to resolve virtual method 250: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
06-14 19:19:45.691  10616-10616/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x00c2
06-14 19:19:45.691  10616-10616/? I/dalvikvm﹕ DexOpt: access denied from Lcom/google/android/gms/common/GooglePlayServicesUtil; to field Landroid/app/Notification;.extras
06-14 19:19:45.691  10616-10616/? W/dalvikvm﹕ VFY: unable to resolve instance field 18
06-14 19:19:45.691  10616-10616/? D/dalvikvm﹕ VFY: replacing opcode 0x54 at 0x00e1
06-14 19:19:45.691  10616-10616/? E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
06-14 19:19:45.691  10616-10616/? W/dalvikvm﹕ VFY: unable to resolve check-cast 27 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/common/GooglePlayServicesUtil;
06-14 19:19:45.691  10616-10616/? D/dalvikvm﹕ VFY: replacing opcode 0x1f at 0x000e
06-14 19:19:45.696  10616-10616/? I/dalvikvm﹕ Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzh
06-14 19:19:45.696  10616-10616/? W/dalvikvm﹕ VFY: unable to resolve virtual method 542: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
06-14 19:19:45.696  10616-10616/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000b
06-14 19:19:45.696  10616-10616/? W/GooglePlayServicesUtil﹕ Google Play services out of date.  Requires 7571000 but found 2012110

我完全卡住了。任何帮助将不胜感激。

您的对话框未显示,因为在创建对话框时 UI 线程中的执行不会暂停。这意味着您的应用程序将在应该显示对话框后继续 运行。如果您的代码执行以下操作:

showDialog();
startDifferentActivity();

用户将无法与对话框交互,因为不同的 activity 甚至会在对话框显示之前启动。如果您的代码执行依赖于用户选择对话框的 positive/negative/neutral 按钮或关闭对话框,请实施适当的侦听器。