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