Подтвердить что ты не робот

Как настроить ошибку Google Play Services на Android

При использовании нового API Google Maps V2 на Android пользователь увидит сообщение об ошибке, если на их устройстве не установлено приложение Google Play (Сервисы). Мне интересно, возможно ли каким-то образом переопределить стиль этого сообщения об ошибке, чтобы сделать его менее резким и подходящим для стиля приложения более подходящим образом.

Это выглядит так:

This app won't run unless you update Google Play services.

4b9b3361

Ответ 1

После некоторого расследования я решил, что лучшим решением было вручную проверить наличие библиотеки Google Play Services и отобразить собственное диалоговое окно ошибок или макет ошибки. В 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
                }
            }
        });
    }
}

Ответ 2

  • 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 show an ultimate warning.
                MyCustomPlayServicesErrorDialogFragment playServicesErrorDialog = new MyCustomPlayServicesErrorDialogFragment();
                playServicesErrorDialog.show(activity.getFragmentManager(), TAG);
            }
            return false;
        }
        return true;
    }