1. 检查 Google Play 服务
据记录here http://developer.android.com/google/play-services/setup.html#ensure您应该始终确保:
- Google Play Services APK 已存在。
- 安装了正确的版本。
大多数情况下,您会在onResume()
方法。这GooglePlayServiceUtils
有一个方法叫做isGooglePlayServicesAvailable(...)
它会检查 Google Play 服务是否已安装以及版本是否正确。它返回一个int
错误代码可以大致告诉您出了什么问题:
@Override
public void onResume() {
super.onResume();
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
switch(errorCode) {
case ConnectionResult.SUCCESS:
// Google Play Services installed and up to date
break;
case ConnectionResult.SERVICE_MISSING:
// Google Play services is missing on this device.
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
// The installed version of Google Play services is out of date.
break;
case ConnectionResult.SERVICE_DISABLED:
// The installed version of Google Play services has been disabled on this device.
break;
case ConnectionResult.SERVICE_INVALID:
// The version of the Google Play services installed on this device is not authentic.
break;
case ConnectionResult.DATE_INVALID:
// The device date is likely set incorrectly.
break;
}
}
您可以使用类似的方法showErrorDialogFragment(...)
of the GooglePlayServiceUtils
显示适当的错误消息:
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
if(GooglePlayServiceUtil.showErrorDialogFragment(errorCode, getActivity(), REQUEST_CODE)) {
// Dialog was shown
} else {
// Dialog was not shown.
}
仅当错误代码不是以下内容时才会显示该对话框ConnectionResult.SUCCESS
.
您可以找到相关文档GooglePlayServiceUtil
class here http://developer.android.com/reference/com/google/android/gms/common/GooglePlayServicesUtil.html.
2. Fallback
您是否需要后备仅取决于您的应用程序和您的目标市场。例如,如果您将其发布到 Google Play 商店(我假设您在使用 Google Play 服务后就这样做了),那么您实际上并不需要后备。因为人们要下载您的应用程序,他们首先需要 Google Play 商店,如果他们拥有 Google Play 商店,那么他们就可以访问 Google Play 服务。如果您想在亚马逊设备或诺基亚商店中发布它,那当然是一个完全不同的故事,因为拥有这些设备的设备没有 Google Play 商店或 Google Play 服务。同样在像中国这样的市场,几乎没有任何 Android 设备具有 Google Play 商店或任何 Google 服务,因此本质上您必须决定您的应用程序是否有必要,但正如我所说,如果您将其发布到 Google Play 商店,我不会不用担心。我上面提到的检查和错误对话框应该足以确保您的应用程序正常运行。新版本的 Google Play Services APK 的推出通常非常快。这可能需要几天的时间,但之后大多数设备都会收到更新。您还必须考虑一件事:如果您开发的应用程序需要当前 Google Play 服务的功能,那么您很可能不会遇到任何问题,因为几乎所有设备都已经具有当前版本,因此将相当困难找到不是最新的设备。当您快速推出使用几天前推出的某些新功能的应用程序更新时,您必须小心。在这种情况下,您可能会遇到更多问题。
但最终您必须记住,这一切都取决于您发布应用程序的国家/地区以及您想要发布应用程序的生态系统。
我希望我能回答您的问题,如果您还有其他问题,请随时提问!