13

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

这是错误的样子:

除非您更新 Google Play 服务,否则此应用将无法运行。

4

3 回答 3

24

在进行了一些调查后,我确定最好的解决方案是手动检查 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
                }
            }
        });
    }
}
于 2012-12-20T22:02:16.347 回答
2

特瓦丁顿的回答很棒。但是,当我尝试使用它时,该方法已被弃用。因此,经过大量研究,我创建了一个名为的函数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;
    }

我希望它会节省你很多宝贵的时间。PS-我刚刚开始在 StackOverflow 上回答,请让我知道您对此答案的建议。

于 2018-07-05T15:45:53.647 回答
1
  • GooglePlayServiceUtil已弃用。查看GoogleApiAvailability最新的接口。
  • 更喜欢直接使用提供的DialogFragment而不是错误AlertDialog,以便它可以由活动正确管理。

    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;
    }
    
于 2015-10-25T01:26:56.410 回答