In my application, I need to check Google Play services version (which is installed in user\'s device). Is it possible ? And if yes, how can I do that? I searched Google but
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.
use the following function to check if google play services could be used or not
private boolean checkGooglePlayServicesAvailable() {
final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
return false;
}
return true;
}
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 ));
Updated (2019.07.03) Kotlin version:
class GooglePlayServicesUtil {
companion object {
private const val GOOGLE_PLAY_SERVICES_AVAILABLE_REQUEST = 9000
fun isGooglePlayServicesWithError(activity: Activity, showDialog: Boolean): Boolean {
val status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)
return if (status != ConnectionResult.SUCCESS) {
if (showDialog) {
GoogleApiAvailability.getInstance().getErrorDialog(activity, status, GOOGLE_PLAY_SERVICES_AVAILABLE_REQUEST).show()
}
true
} else {
false
}
}
}
}
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}
From newer updates i have ended up with this code, i have made a handy method to manage all stuff related to it..
All details related to availability of Service and related details are available here.
private void checkPlayService(int PLAY_SERVICE_STATUS)
{
switch (PLAY_SERVICE_STATUS)
{
case ConnectionResult.API_UNAVAILABLE:
//API is not available
break;
case ConnectionResult.NETWORK_ERROR:
//Network error while connection
break;
case ConnectionResult.RESTRICTED_PROFILE:
//Profile is restricted by google so can not be used for play services
break;
case ConnectionResult.SERVICE_MISSING:
//service is missing
break;
case ConnectionResult.SIGN_IN_REQUIRED:
//service available but user not signed in
break;
case ConnectionResult.SUCCESS:
break;
}
}
I use it like this,
GoogleApiAvailability avail;
int PLAY_SERVICE_STATUS = avail.isGooglePlayServicesAvailable(this);
checkPlayService(PLAY_SERVICE_STATUS);
And for version GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE;
will give you.
And one of the most useful answer i found during my research is here.