我尝试在使用Google Play服务APK之前检查其可用性。我的设备上的软件包已过期(日志显示“...Google Play服务已过期。需要3225100但找到3136134”)。
下面的代码将处理这种情况,并显示一个对话框,提示用户进行更新。
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
立即返回,不显示对话框(并且不阻塞UI事件上的UI线程)。
您能否阐明可能发生的情况以及如何纠正代码以显示对话框?
@Override
protected void onResume() {
super.onResume();
// Check device for Play Services APK. If check succeeds, proceed with
// GCM registration.
if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);
if (regid == null || regid.length() == 0) {
registerInBackground();
} else {
this.user.setGCMRegistrationId(regid);
}
} else {
Log.i(TAG, "No valid Google Play Services APK found.");
}
}
/**
* Check the device to make sure it has the Google Play Services APK. If
* it doesn't, display a dialog that allows users to download the APK from
* the Google Play Store or enable it in the device's system settings.
*/
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Log.i(TAG, "This device is not supported.");
finish();
}
return false;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PLAY_SERVICES_RESOLUTION_REQUEST:
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Google Play Services must be installed.",
Toast.LENGTH_SHORT).show();
finish();
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
1条答案
按热度按时间w80xi6nr1#
当我在项目中错误地包含Google Play服务库时,我遇到了一些与您的代码类似的奇怪行为。您必须 * 将
google-play-services_lib
目录作为项目导入 *,并且 * 必须在类路径中包含google-play-services.jar。原因是该项目包含大量资源,包括显示getErrorDialog()
相应对话框所需的资源。