2

NOTE: I am not trying to update google play services in the emulator. I do not care that it is out of date. I mentioned it only to show that somehow logcat is reporting the actual build number, which I wish to access in the app.

Similar, but not the same as, How can I determine the version of Google Play services?

An app I'm developing using Android Studio 1.2.2 is experiencing a problem if the latest Google Play Services is not installed on the device, yet GoogleApiAvailability is not reporting a problem and so the code instructing the user to update is never called. If I manually tell the users to update play services in the play store with a manual link to https://play.google.com/store/apps/details?id=com.google.android.gms&hl=en, there is an update available, and the app functions correctly after they install it.

However I am unable to find a way to determine that the user needs to update via application code or the gradle build file.

  1. In the gradle file I've specified: compile 'com.google.android.gms:play-services:7.5.0' and this is the latest version as far as I know. Android Studio does not indicate that I should update this line to a newer version.

  2. The SDK manager reports the play services I have installed is "rev 25", and no update is available.

  3. When I test in an emulator, the code works correctly, and in logcat I see the message: "W/GooglePlayServicesUtil: Google Play services out of date. Requires 7571000 but found 6774470". This is normal for the emulator since they haven't released new images yet, but it provides an interesting clue.

Is there a way to get this build number reported in item 3 above, programatically? If so, I could compare against that rather than using the isgooglePlayServicesAvailable method of GoogleApiAvailability -- which I'm already using, but is reporting success on devices that need an update.

Community
  • 1
  • 1
alzee
  • 1,393
  • 1
  • 10
  • 21
  • The emulator does not have google play services installed or the correct version. – Jared Burrows Jul 30 '15 at 14:18
  • possible duplicate of [How to download Google Play Services in an Android emulator?](http://stackoverflow.com/questions/14536595/how-to-download-google-play-services-in-an-android-emulator) – Jared Burrows Jul 30 '15 at 14:18
  • 1
    Not a duplicate, not concerned with updating the emulator. Please read the entire question. – alzee Jul 30 '15 at 14:19

2 Answers2

0

I've managed to do a little digging and answer my own question. The following code will do what I (and perhaps others) want.

PackageInfo pi = getPackageManager().getPackageInfo("com.google.android.gms", 0);
if (pi.versionCode < VERSION_YOU_WANT)
{
    // instruct user to update
}

What I've done is wrap this in the required exception handler and run it if the API check reports SUCCESS. If the version is too low I call the getErrorDialog just like when the API check fails, with ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED instead of the returned SUCCESS value.

Doing this may require you to update your minimum SDK target. For example both my phone(5.0.1) and tablet(4.2.2) report success from the API check, and going to the play store does not show an update for either one; However, the installed build on the table is 7895032 while on the phone it's 7895438.

alzee
  • 1,393
  • 1
  • 10
  • 21
-1

This function will tell the user to update the GooglePlaServices if an update is available. You can start the registration process, if this function returns true

private boolean checkPlayServices()
{
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (resultCode != ConnectionResult.SUCCESS)
    {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode))
            GooglePlayServicesUtil.getErrorDialog(resultCode, this, 9000).show();

        else
        {
            Log.i(TAG, "This device is not supported.");
            finish();
        }

        return false;
    }

    return true;
}
Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
  • Please read the question. I'm already doing exactly that. It is reporting SUCCESS in cases where there is a newer version in the app store that fixes a bug. – alzee Jul 30 '15 at 14:26
  • Is this useful? https://developers.google.com/android/reference/com/google/android/gms/common/GoogleApiAvailability.html#GOOGLE_PLAY_SERVICES_VERSION_CODE – Prerak Sola Jul 30 '15 at 14:45
  • Not really I don't think. I believe that's a static version number, not one that will change by device, but I could be wrong. In any case I found a way to get the version number I need from the package manager, see my answer below. – alzee Jul 30 '15 at 15:29