GooglePlayServicesUtil vs GoogleApiAvailability

Android

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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).

Solution 3 - Android

You will have to use GoogleApiAvailability instead:

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

this represents the context.

Solution 4 - Android

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

Solution 5 - Android

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

Solution 6 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJamesView Question on Stackoverflow
Solution 1 - AndroidJamesView Answer on Stackoverflow
Solution 2 - AndroidAlexander FarberView Answer on Stackoverflow
Solution 3 - AndroidGauravView Answer on Stackoverflow
Solution 4 - AndroidAnoop M MaddasseriView Answer on Stackoverflow
Solution 5 - AndroidArpit AnandView Answer on Stackoverflow
Solution 6 - AndroidMahmoud MabrokView Answer on Stackoverflow