How to check Google Play services version?
IMPORTANT: GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE returns the minimum version required by YOUR app, not the play services version that is installed on the user's device.
If you look in the link provided by Stan0 you will see this:
public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE
Minimum Google Play services package version (declared in AndroidManifest.xml android:versionCode) in order to be compatible with this client version. Constant Value: 3225000 (0x003135a8)
So, when you set that in your manifest and then call isGooglePlayServicesAvailable(context)
:
public static int isGooglePlayServicesAvailable (Context context)
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.
Returns
- status code indicating whether there was an error. Can be one of following in ConnectionResult:
SUCCESS
,SERVICE_MISSING
,SERVICE_VERSION_UPDATE_REQUIRED
,SERVICE_DISABLED
,SERVICE_INVALID
.
It will ensure that the device is using the version your app requires, if not, then you can take action according to the documentation
Edit
GooglePlayServicesUtil.isGooglePlayServicesAvailable()
is deprecated and now GoogleApiAvailability.isGooglePlayServicesAvailable()
should be used instead.
I found simple solution:
int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;
But versionCode
is deprecated in API 28, so you can use PackageInfoCompat
:
long v = PackageInfoCompat.getLongVersionCode(getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ));
Here is my solution:
private boolean checkGooglePlayServices() {
final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status != ConnectionResult.SUCCESS) {
Log.e(TAG, GooglePlayServicesUtil.getErrorString(status));
// ask user to update google play services.
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, 1);
dialog.show();
return false;
} else {
Log.i(TAG, GooglePlayServicesUtil.getErrorString(status));
// google play services is updated.
//your code goes here...
return true;
}
}
and you have two options, either write your code directly in else block as commented, or use the returned boolean value for this method to write custom code.