【问题标题】:How to style the Google Play Services error on Android如何在 Android 上设置 Google Play Services 错误样式
【发布时间】:2012-12-20 19:36:47
【问题描述】:

在 Android 上使用新的 Google Maps V2 API 时,如果用户的设备未安装 Google Play(服务)应用,则会看到一条错误消息。我想知道是否有可能以某种方式覆盖此错误消息的样式,以使其不那么刺耳并更适合应用程序样式。

这是错误的样子:

【问题讨论】:

  • 你是怎么提出来的? getErrorDialog() 的结果并没有那么令人震惊。话虽如此,我还没有看到任何文件记录会影响 UI 的这一部分。
  • 我只是将地图片段嵌入到 XML 布局中。如果缺少播放服务,它会显示此内容。我不知道错误对话框选项!这似乎是一个很好的解决方案。
  • 是的,我会选择getErrorDialog() 路线。 Maps V2 体验的整个部分都很糟糕,但在我看来,getErrorDialog() 最糟糕。 :-)

标签: android google-play-services


【解决方案1】:

在进行了一些调查后,我确定最好的解决方案是手动检查是否存在 Google Play 服务库并显示自定义错误对话框或错误布局。 GooglePlayServicesUtil 中有一些实用方法可以让这变得相当简单。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int statusCode =
            GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (statusCode == ConnectionResult.SUCCESS) {
        // Continue with your regular activity/fragment configuration.
    } else {
        // Hide the map fragment so the default error message is not
        // visible.    
        findViewById(R.id.map).setVisibility(View.GONE);

        // Show a custom error message
        showErrorMessage(statusCode);
    }
}

private void showErrorMessage(final int statusCode) {
    // I've outlined two solutions below. Pick which one works best for
    // you and remove the if-block.
    boolean showDialog = false;

    if (showDialog) {
        // This is the easiest method and simply displays a pre-configured
        // error dialog
        GooglePlayServicesUtil.getErrorDialog(statusCode, this, 0).show();
    } else {
        // Show a completely custom layout
        findViewById(R.id.error).setVisibility(View.VISIBLE);

        // Wire up the button to install the missing library
        Button errorButton = (Button) findViewById(R.id.error_button);
        errorButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    // Perform the correct action for the given status
                    // code!
                    GooglePlayServicesUtil.getErrorPendingIntent(
                            statusCode, getActivity(), 0).send();
                } catch (CanceledException e1) {
                    // Pass
                }
            }
        });
    }
}

【讨论】:

  • 以及如何为按钮设置合适的文字?
  • 要显示警报,最好先检查GooglePlayServicesUtil.isUserRecoverableError(statusCode)
【解决方案2】:

Twadington 的回答很棒。但是,当我尝试使用它时,该方法已被弃用。因此,经过大量研究,我创建了一个名为 isPlayServicesAvailable() 的函数,您可以在使用任何使用 Play 服务的功能之前检查它。它会根据错误自动创建对话框,如果问题可以解决,还会链接到 Play 商店。

public boolean isGooglePlayServicesAvailable(Activity activity) {
        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
        int status = googleApiAvailability.isGooglePlayServicesAvailable(activity);
        if(status != ConnectionResult.SUCCESS) {
            if(googleApiAvailability.isUserResolvableError(status)) {
                googleApiAvailability.getErrorDialog(this, status, 0, new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        finish();
                    }
                }).show();
            }
            else {Toast.makeText(this, "This app can not run on this device", Toast.LENGTH_LONG).show();
            }
            return false;
        }
        return true;
    }

我希望它可以节省您的宝贵时间。 P.S.-我刚刚开始在 StackOverflow 上回答,请告诉我您对此答案的建议。

【讨论】:

    【解决方案3】:
    • GooglePlayServiceUtil 已弃用。查看GoogleApiAvailability 了解最新接口。
    • 优先使用提供的DialogFragment,而不是直接使用错误AlertDialog,这样可以被activity妥善管理。

      public static boolean checkPlayServices(FragmentActivity activity) {
          GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
          int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(activity);
      
          if (resultCode != ConnectionResult.SUCCESS) {
              if (googleApiAvailability.isUserResolvableError(resultCode)) {
                  // "user resolvable" means Google Play is available to download the last version of Play Services APK
                  // This will open Google dialog fragment displaying the proper message depending on "resultCode"
                  googleApiAvailability.showErrorDialogFragment(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST);
              } else {
                  // Should not happen. This device does not support Play Services.
                  // Let's show an ultimate warning.
                  MyCustomPlayServicesErrorDialogFragment playServicesErrorDialog = new MyCustomPlayServicesErrorDialogFragment();
                  playServicesErrorDialog.show(activity.getFragmentManager(), TAG);
              }
              return false;
          }
          return true;
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 2020-09-16
      • 1970-01-01
      • 2015-02-12
      • 2017-06-22
      • 2015-06-25
      • 1970-01-01
      相关资源
      最近更新 更多