Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GooglePlayServicesUtil vs GoogleApiAvailability

Tags:

android

I am trying to use Google Play Service in my Android app. As Google document says, we need to check if the Google API is available before using it. I have searched some way to check it. Here is what I got:

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

But when I go to Google Api GooglePlayServicesUtil page, https://developers.google.com/android/reference/com/google/android/gms/common/GooglePlayServicesUtil

I find all functions are deprecated. For example, the method

GooglePlayServicesUtil.isGooglePlayServicesAvailable (deprecated)

And Google recommends to use:

GoogleApiAvailability.isGooglePlayServicesAvailable.

However, when I try to use GoogleApiAvailability.isGooglePlayServicesAvailable, I get the error message:

enter image description here

like image 518
James Avatar asked Jun 24 '15 02:06

James


People also ask

How to verify that Google Play Services is installed and enabled?

Use GoogleApiAvailability.isGooglePlayServicesAvailable (Context) instead. Verifies that Google Play services is installed and enabled on this device, and that the version installed on this device is no older than the one required by this client.

What does isgoogleplayservicesavailable (context) do?

It will direct them to one of the following places to either the Play Store if Google Play services is out of date or missing, or system settings if Google Play services is disabled on the device. error code returned by isGooglePlayServicesAvailable (Context) call.

How do I resolve an error in Google Play Services?

Determines whether an error can be resolved via user action. If true, proceed by calling GoogleApiAvailability.getErrorDialog (Activity, int, int) and showing the dialog. Attempts to make Google Play services available on this device. If Play Services is already available, the returned Task may complete immediately.

How do I get error notifications from googleapiavailability?

Use GoogleApiAvailability.showErrorNotification (Context, int) instead. Displays a notification relevant to the provided error code. This method is similar to getErrorDialog (int, android.app.Activity, int), but is provided for background tasks that cannot or shouldn't display dialogs.


1 Answers

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; } 
like image 127
James Avatar answered Sep 16 '22 15:09

James