Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Play Services Installed and Play Services Used in App

What I Have

I have an app that heavily relies on Google Play Services (for Firebase), so I need the user's device to have Play Services installed. More importantly, the Play Services version installed in the device should be equal to or higher than the Play Services I am using in my app.

What I Want

I want to show a message (maybe a dialog or snackbar) in the Splash screen of my app if the user is using an old Play Services version that the version I am targeting in my app.

Like currently, the version of Play Services I am using in my app is 10.0.1. This is the version name obviously.

I know I can do this,

int v = getPackageManager().getPackageInfo("com.google.android.gms", 0 ).versionName;

But I am unable to compare the versions of the Play Services installed in the device and the Play Services I am using? Should I be doing it using the versionCode instead of the versionName? If so, how do I know the versionCode of the Play Services I am using?

Or is there a different (or better) way to do this?

like image 716
Aritra Roy Avatar asked Dec 10 '22 13:12

Aritra Roy


2 Answers

This is method i use .. cheers

public static boolean checkPlayServices(Context context) {
    final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int resultCode = api.isGooglePlayServicesAvailable(context);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (api.isUserResolvableError(resultCode))
            api.getErrorDialog(((Activity) context), resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
        else {
            showToast(context, "This device is not supported.", true);
            ((Activity) context).finish();
        }
        return false;
    }
    return true;
}
like image 60
Usman Ghauri Avatar answered May 23 '23 20:05

Usman Ghauri


Usman's answer is good, additionally you can call GoogleApiAvailability.makeGooglePlayServicesAvailable() in the event of a non-successful result code. Like this:

int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
    GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(this);
}

Further details about result codes, etc. available in docs here: https://developers.google.com/android/reference/com/google/android/gms/common/GoogleApiAvailability

like image 36
Deemoe Avatar answered May 23 '23 19:05

Deemoe