2012-12-20 45 views
13

在Android上使用新的Google Maps V2 API時,如果用戶的設備未安裝Google Play(服務)應用程序,用戶將看到一條錯誤消息。我想知道是否有可能以某種方式重寫此錯誤消息的樣式,使它更少刺激,並適合更合適的應用程序樣式。如何在Android上設置Google Play服務錯誤風格

這是錯誤的樣子:

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

+1

你是如何使這一嗎? 'getErrorDialog()'的結果並不令人感到震驚。話雖如此,我還沒有看到任何影響這部分用戶界面的記錄。 – CommonsWare

+0

我只是將地圖片段嵌入到XML佈局中。如果播放服務丟失,它將顯示此內容。我不知道錯誤對話框選項!這似乎是一個很好的解決方案。 – twaddington

+1

是的,我會去'getErrorDialog()'路線。整個Maps V2體驗都很糟糕,但'getErrorDialog()'對我來說似乎最不吸引人。 :-) – CommonsWare

回答

24

後做了一些調查,我下定決心,最好的解決辦法是手動檢查谷歌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 
       } 
      } 
     }); 
    } 
} 
+0

以及如何設置適合的按鈕文本? –

+0

要顯示警報,最好先檢查「GooglePlayServicesUtil.isUserRecoverableError(statusCode)」 – bryant1410

0
  • 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; 
    }