GooglePlayServicesUtil与GoogleApiAvailability

我正在尝试在我的Android应用中使用Google Play服务。 正如Google文档所说,我们需要在使用之前检查Google API是否可用。 我已经search了一些方法来检查它。 这是我得到的:

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

但是当我转到Google Api GooglePlayServicesUtil页面时, https://developers.google.com/android/reference/com/google/android/gms/common/GooglePlayServicesUtil

我发现所有的function都被弃用了 。 例如,该方法

GooglePlayServicesUtil.isGooglePlayServicesAvailable (已弃用)

Googlebuild议使用:

GoogleApiAvailability.isGooglePlayServicesAvailable

但是,当我尝试使用GoogleApiAvailability.isGooglePlayServicesAvailable时,我收到错误消息:

在这里输入图像描述

我find了解决办法。 在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; } 

不能再使用GooglePlayServicesUtil类了!

以下是GoogleApiAvailability类可以如何使用 – 例如,当需要GCM(或任何其他Google服务)时:

 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() )。

您将不得不使用GoogleApiAvailability

 GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); int errorCode = googleApiAvailability.isGooglePlayServicesAvailable(this); 

this代表了context

检查设备,确保它具有Google Play服务APK。 如果没有,则显示一个对话框,允许用户从Google Play商店下载APK,或者在设备的系统设置中启用它。

 public static boolean checkPlayServices(Activity activity) { final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) .show(); } else { Logger.logE(TAG, "This device is not supported."); } return false; } return true; }