2012-12-20 38 views
13

Khi sử dụng API Google Maps V2 mới trên Android, người dùng sẽ thấy thông báo lỗi nếu thiết bị của họ chưa cài đặt ứng dụng Google Play (Dịch vụ). Tôi tự hỏi nếu nó có thể bằng cách nào đó ghi đè lên phong cách của thông báo lỗi này để làm cho nó ít chói tai và phù hợp với phong cách ứng dụng một cách thích hợp hơn.Cách tạo lỗi dịch vụ của Google Play trên Android

Đây là những gì lỗi trông giống như:

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

+1

Bạn sẽ làm điều đó như thế nào? Kết quả từ 'getErrorDialog()' không gần như là chói tai. Điều đó đang được nói, tôi đã không thấy bất cứ điều gì được ghi lại để ảnh hưởng đến phần này của giao diện người dùng. – CommonsWare

+0

Tôi chỉ có đoạn bản đồ được nhúng trong một bố cục XML. Nó hiển thị nội dung này nếu các dịch vụ phát bị thiếu. Tôi không biết về tùy chọn hộp thoại lỗi! Điều đó có vẻ như một giải pháp tốt. – twaddington

+1

Vâng, tôi sẽ đi đường 'getErrorDialog()'. Toàn bộ phần trải nghiệm Maps V2 này hút, nhưng 'getErrorDialog()' dường như khiến tôi ít nhất phải chịu đựng. :-) – CommonsWare

Trả lời

24

Sau khi thực hiện một số điều tra, tôi xác định rằng giải pháp tốt nhất là tự kiểm tra sự hiện diện của các thư viện Google Play Services và hiển thị một lỗi tùy chỉnh hộp thoại hoặc bố cục lỗi. Có một số phương pháp tiện ích trong GooglePlayServicesUtil mà làm cho điều này khá đơn giản.

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

Và cách đặt văn bản phù hợp cho nút? –

+0

Để hiển thị cảnh báo, tốt hơn nên kiểm tra 'GooglePlayServicesUtil.isUserRecoverableError (statusCode) đầu tiên' – bryant1410

0
  • GooglePlayServiceUtil bị phản đối. Nhìn vào GoogleApiAvailability cho các giao diện mới nhất.
  • Ưu tiên sử dụng số DialogFragment được cung cấp thay vì trực tiếp lỗi AlertDialog để có thể hoạt động đúng cách.

    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; 
    } 
    
Các vấn đề liên quan