我正在尝试在我的Android应用程序中使用Google Play服务。正如Google文档所说,在使用Google API之前,我们需要检查它是否可用。我已经寻找了一些方法来检查它。下面是我得到的信息:
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;
}
但是当我去谷歌Api GooglePlayServicesUtil页面时,https://developers.google.com/android/reference/com/google/android/gms/common/GooglePlayServicesUtil
我发现所有的函数都是弃用的。例如,方法
GooglePlayServicesUtil.isGooglePlayServicesAvailable (已弃用)
谷歌推荐使用:
GoogleApiAvailability.isGooglePlayServicesAvailable.
然而,当我尝试使用GoogleApiAvailability.isGooglePlayServicesAvailable,时,我得到了错误消息:
发布于 2015-06-24 10:45:46
我已经找到了解决方案。在GoogleApiAvailability
中,所有方法都是公共方法,而在GooglePlayServicesUtil
中,所有方法都是静态公共函数。
因此,要使用GoogleApiAvailability,正确的方法是:
private boolean checkPlayServices() {
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int result = googleAPI.isGooglePlayServicesAvailable(this);
if(result != ConnectionResult.SUCCESS) {
if(googleAPI.isUserResolvableError(result)) {
googleAPI.getErrorDialog(this, result,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
}
return false;
}
return true;
}
发布于 2015-09-17 23:18:48
不应该再使用GooglePlayServicesUtil类了!
下面是如何使用类GoogleApiAvailability -例如,当需要GCM (或任何其他谷歌服务)时:
public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
startRegistrationService();
}
}
private void startRegistrationService() {
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
int code = api.isGooglePlayServicesAvailable(this);
if (code == ConnectionResult.SUCCESS) {
onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null);
} else if (api.isUserResolvableError(code) &&
api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) {
// wait for onActivityResult call (see below)
} else {
Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case REQUEST_GOOGLE_PLAY_SERVICES:
if (resultCode == Activity.RESULT_OK) {
Intent i = new Intent(this, RegistrationService.class);
startService(i); // OK, init GCM
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
更新:
REQUEST_GOOGLE_PLAY_SERVICES
是可以在onActivityResult()
方法中引用的具有任意名称和值的整数常量。
此外,在上面的代码中调用this.onActivityResult()
也是可以的(您也可以在其他地方调用super.onActivityResult()
)。
发布于 2016-11-01 04:04:24
您将不得不改用GoogleApiAvailability:
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int errorCode = googleApiAvailability.isGooglePlayServicesAvailable(this);
this
代表context
。
https://stackoverflow.com/questions/31016722
复制相似问题