2012-12-20 19 views
13

Podczas korzystania z nowego interfejsu API Map Google V2 na Androida użytkownik zobaczy komunikat o błędzie, jeśli na jego urządzeniu nie ma zainstalowanej aplikacji Google Play (usługi). Zastanawiam się, czy możliwe jest jakoś nadpisanie stylu tego komunikatu o błędzie, aby było mniej drażniące i lepiej pasowało do stylu aplikacji.Sposób stylizowania błędu Usług Google Play na Androida

To, co wygląda na błąd jak:

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

+1

Jak ty przynosząc że się? Rezultat 'getErrorDialog()' nie jest prawie taki słaby. Mówiąc to, nie widziałem niczego udokumentowanego za wpływanie na tę część interfejsu. – CommonsWare

+0

Po prostu fragment mapy osadzony w układzie XML. Wyświetla tę zawartość, jeśli brakuje usług odtwarzania. Nie wiedziałem o opcji okna dialogowego błędu! To wydaje się dobrym rozwiązaniem. – twaddington

+1

Tak, idę na ścieżkę 'getErrorDialog()'. Cała ta część doświadczenia Map V2 jest do dupy, ale "getErrorDialog()" wydawało mi się najmniej ssać. :-) – CommonsWare

Odpowiedz

24

Po wykonaniu niektórych dochodzenie, uznałem, że najlepszym rozwiązaniem było ręcznie sprawdzić obecność biblioteki Google Play i wyświetli błąd niestandardowy dialog lub układ błędów. Istnieją pewne metody użytkowe w GooglePlayServicesUtil, które sprawiają, że jest to dość proste.

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 
       } 
      } 
     }); 
    } 
} 
+0

A jak ustawić odpowiedni tekst dla przycisku? –

+0

Aby wyświetlić alert, lepiej najpierw sprawdzić 'GooglePlayServicesUtil.isUserRecoverableError (statusCode)' – bryant1410

0
  • GooglePlayServiceUtil jest przestarzała. Spójrz na GoogleApiAvailability, aby uzyskać najnowsze interfejsy.
  • Zamiast korzystać bezpośrednio z błędu DialogFragment, można go odpowiednio zarządzać za pomocą działania.

    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; 
    } 
    
Powiązane problemy