GooglePlayServicesUtil VS GoogleApiUpability

我正在尝试在我的 Android 应用程序中使用 Google Play Service。正如 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

我发现所有的函数都是 不赞成

IsGooglePlayServicesUpable (弃用)

谷歌建议使用:

IsGooglePlayServices.

但是,当我尝试使用 GoogleApiiliability.isGooglePlayServices  ,我得到了一个错误消息:

enter image description here

60397 次浏览

I have found the solution. In the GoogleApiAvailability, all methods are public method, while in GooglePlayServicesUtil all methods are static public function.

So to use GoogleApiAvailability, the right way is:

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

The class GooglePlayServicesUtil shouldn't be used anymore!

Here is how the class GoogleApiAvailability can be used instead - when for example GCM (or any other Google service) is needed:

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

UPDATE:

REQUEST_GOOGLE_PLAY_SERVICES is an integer constant with arbitrary name and value, which can be referred to in the onActivityResult() method.

Also, calling this.onActivityResult() in the above code is okay (you also call super.onActivityResult() in the other place).

You will have to use GoogleApiAvailability instead:

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

this represents the context.

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.

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

I have added this as fun in BaseActivity class to be used in all places

    fun checkGooglePlayServices(okAction : ()-> Unit , errorAction: (msg:String, isResolved:Boolean)-> Unit){
val apiAvailability = GoogleApiAvailability.getInstance()
val resultCode = apiAvailability.isGooglePlayServicesAvailable(this)
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
apiAvailability.getErrorDialog(
this,
resultCode,
PLAY_SERVICES_RESOLUTION_REQUEST
).show()
// dialoe when click on ok should let user go to install/update play serices




errorAction("dialog is shown" , true)


} else {
"checkGooglePlayServices  This device is not supported.".log(mTag)
errorAction("This device is not supported",false)
}
}else{
okAction()
}
}


companion object {
const val PLAY_SERVICES_RESOLUTION_REQUEST = 1425
}

use it like this

    (activity as? BaseActivity)?.checkGooglePlayServices({
// ok so start map
initializeMap()
},
{ msg, isResolved ->
if (!isResolved)
context?.show(msg)


}
)

Or you can customize it as you want.

Just putting a .getInstance() in between the two methods would do the game.