如何设置Android上的Google Play服务错误样式

twa*_*ton 13 android google-play-services

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

这就是错误的样子:

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

twa*_*ton 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
                }
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)