4

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?

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
  • do you want to know the google play Services versionCode ? of a mobile? – Charuක Feb 02 '17 at 14:55
  • Did you even read the question? – Aritra Roy Feb 02 '17 at 14:57
  • ok whatever trying to help you out with my bad english.anyway good luck – Charuක Feb 02 '17 at 14:58
  • Hi. Isn't what was mentioned in the docs about [Check for Google Play Services](https://firebase.google.com/docs/cloud-messaging/android/client#sample-play) what you're looking for? :) – AL. Feb 03 '17 at 03:41
  • @AL I came straight from those docs to here, because it does not say HOW to check, only recommends THAT you check. OP is asking how to check. – Jannie Theunissen Oct 23 '17 at 07:09
  • @JannieTheunissen I'm interpreting the all-caps word as something you're saying in a *shouting tone*, which isn't good. I'm not really sure where this tone is coming from, if that *is* what the OP was asking, I would've appreciated if *he* said so, then I would've helped him with something else. – AL. Oct 23 '17 at 07:21
  • Hey, no shouting or tone intended. Just clumsily forgot that comments also support markdown. Was just trying to emphasize for clarity. Since you were asking a question and OP wasn't answering, I offered an answer. Maybe to save others from an unnecessary detour in the docs you are referring to. ;-) – Jannie Theunissen Oct 23 '17 at 07:34

2 Answers2

11

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;
}
Usman Ghauri
  • 931
  • 1
  • 7
  • 26
  • 4
    To expand this answer a little bit, you can check the following condition `resultCode == SERVICE_VERSION_UPDATE_REQUIRED`. For a complete list of result codes and their meaning you can check [the docs](https://developers.google.com/android/reference/com/google/android/gms/common/ConnectionResult) – Nicolás Carrasco-Stevenson Feb 02 '17 at 15:09
  • @NicolásCarrasco yes, it can be considered as `success` unless something can only be done using the latest version. – Hamzeh Soboh Dec 18 '17 at 14:53
2

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

Deemoe
  • 931
  • 10
  • 12