isGooglePlayServicesAvailable always returns 2 - android

I'm trying to implement Google Play Services Library to my Android app. But there is an issue about isGooglePlayServicesAvailable function.
Although my play services is up to date, it returns 2 which means SERVICE_VERSION_UPDATE_REQUIRED according to documentation.
My code is below:
#Override
protected void onResume() {
super.onResume();
int statusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(LoginSigninActivity.this);
if( statusCode != ConnectionResult.SUCCESS)
{
Log.e("statuscode",statusCode+"");
if(GooglePlayServicesUtil.isUserRecoverableError(statusCode))
{
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
statusCode,
LoginSigninActivity.this,
REQUEST_CODE_RECOVER_PLAY_SERVICES);
// If Google Play services can provide an error dialog
if (errorDialog != null) {
errorDialog.show();
}
}
else
{
Toast.makeText(this, getString(R.string.toast_google_play_services_not_found),Toast.LENGTH_LONG).show();
}
}
}
Thanks in advance.
P.S. Error dialog is always shown.

No it should not.
Device with android 4.4.4 has play service version 5.0.89 where as devices with android L preview has play service version 5.2.08. This is reason why android studio ask you to update your play service version to 5.2.08. So, for now use
compile 'com.google.android.gms:play-services:5.0.89'
If you are using android L emulator, i think you should use 5.2.08.
And if you want to update your play service to 5.2 see this.

I also faced same problem. I used latest play services version: 5.+ to develop may app. But my device which has Kitkat uses play services version: 4.4.52. It always said SERVICE_VERSION_UPDATE_REQUIRED because i build an app which uses upper version of play service but device has lower version.
My suggestion is use lower play services version to develop your app or use device which uses upper version of play service.

Related

Android Studio says there isnt google play services

I'm developing an android project and until today it was fine. It gets user data and creates a connection via websocket to server. I started the project to check something and it worked normally and 2 minutes later I started t again it stopped getting user location and it said there is a problem with google play sevices
W/GoogleApiManager: The service for com.google.android.gms.common.internal.service.zap is not available: ConnectionResult{statusCode=SERVICE_INVALID, resolution=null, message=null}
W/GooglePlayServicesUtil: com.example.airboxproject requires the Google Play Store, but it is missing.
W/GoogleApiManager: The service for com.google.android.gms.common.internal.service.zap is not available: ConnectionResult{statusCode=SERVICE_INVALID, resolution=null, message=null}
I wouldn't get this messages before but now it keeps producing these error and I don't know what to do. I deleted the emulator and uploaded a new one and now there is difference in it GUI. I have to submit this project in less than 48 hours and I'm having problems with it.
Edit: It shows some parts of the GUI as Android 2 even though it is Android 11
Create a new emulator from Android Studio and verify it has Google Play Installed. Tools > Device Manager > Create Device. Select a device which has the Google Play logo under the Play Store column.
And/or verify the correct emulator is selected next to the run button at the top of Android studio.
It is also best practice to check if the device supports Google Play Services.
Uncomment if (googleAPI.isUserResolvableError(result)) conditional for an automatic dialog to the user explaining the error
public static boolean isPlayServicesInstalled(Context context) {
GoogleApiAvailabilityLight googleAPI = GoogleApiAvailabilityLight.getInstance();
int result = googleAPI.isGooglePlayServicesAvailable(context);
//This call shows a dialog not controlled by the app (dialog text controlled by Firebase package)
// so do not show this as if self handled
// if(googleAPI.isUserResolvableError(result)) {
// Log.v(LOG_TAG, "Google Play Services are not installed");
// GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
// Objects.requireNonNull(googleApiAvailability.getErrorDialog(frag, result, 6)).show();
// }
return result == ConnectionResult.SUCCESS;
}
https://developers.google.com/android/reference/com/google/android/gms/common/GoogleApiAvailability

How to fix apiAvailability.isGooglePlayServicesAvailable misbehaving on emulator

I'm developing an application that uses google maps and I encountered a weird problem when trying to launch my app on emulator - it (obviously) doesn't work and shows an error message "MyApp having trouble with Google Play services. Please try again".
To solve this problem, I decided to check the availability of Google Play Services (as recommended in this guide by Google https://developers.google.com/android/guides/setup). And.. it doesn't work as intended.
So, here's the code I use to check the availability of Google Play Services and the result code for the emulator is always SUCCESS, even though it doesn't have the desired version installed.
private fun checkPlayServices(): Boolean {
val apiAvailability = GoogleApiAvailability.getInstance()
val resultCode = apiAvailability.isGooglePlayServicesAvailable(activity)
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
apiAvailability.getErrorDialog(this.activity, resultCode, 9000)
.show()
} else {
Timber.tag("tag").e("This device is not supported.")
}
return false
}
return true
}
In logs I can clearly see the message
W/GooglePlayServicesUtil: Google Play services out of date. Requires 13400000 but found 13280022
And I cannot comprehend why Google says that there are google play services installed on a version without google play service
I expected this function to return some kind of error and show the correct dialog, but it just returns success for the emulator.
Though, if I run it on a version WITHOUT google API, it shows the error correctly about not having google play services completely.
So, my question is - is it a problem with emulator or API and will it misbehave like this on normal phones or this is just an emulator bug (feature)?

Testing which Google Play Services API is running

When updated code containing a deprecated Android API call, it's relatively straightforward to code for each Android version. e.g.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mTonePlayer = new SoundPool.Builder().build();
} else {
mTonePlayer = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
}
But what if something is deprecated in the Google Play Services API? For example LocationServices.GeofencingAPI was recently deprecated and should be replaced by GeofencingClient.
This will depend on the target device running Google Play services version 11.0.0 and isn't necessarily dependent on the Android SDK. So how can I code for both?
e.g.
if (Google Play Services is below version 11.0.0) {
LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient etc
} else {
GeofencingClient gc = LocationServices.getGeofencingClient();
gc.removeGeofences(); etc
}
You can use the checkApiAvailability() method.
Pay attention since the method returns a task that asynchronously checks if specified APIs are available. If one or more aren't available, the task fails with an AvailabilityException.
Otherwise you can isGooglePlayServicesAvailable to verify 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.

How do I determine the actual build number for Google Play Services on Android?

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.
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.
The SDK manager reports the play services I have installed is "rev 25", and no update is available.
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.
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.
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;
}

When does isGooglePlayServicesAvailable return SERVICE_VERSION_UPDATE_REQUIRED?

According to the documentation GooglePlayServicesUtil.isGooglePlayServicesAvailable returns SERVICE_VERSION_UPDATE_REQUIRED when "The installed version of Google Play services is out of date".
Does this mean that there is a new version of google play services in the play store?
Does this mean that the app needs a newer version than the one that is currently installed in the device?
How is this check done?
this means that the version of google play service you included in your app is higher than the one currently installed on the users device. the user needs to update their google play services in-order for your app to work correctly.
if the result comes back with that error you can simply call this method to alert the user they need to update and it will take them there.
GooglePlayServicesUtil.getErrorDialog(result, this, GOOGLE_PLAY_SERVICE_UPDATE_CODE).show();
result is the result of the isGooglePlayServicesAvailable method
Here are the docs for GooglePlayServicesUtil: http://developer.android.com/reference/com/google/android/gms/common/GooglePlayServicesUtil.html.
Here is where they talking about "ensuring" the user has it installed: https://developer.android.com/google/play-services/setup.html#ensure
This is taken from the Official Iosched 2014 source code here:
https://github.com/google/iosched/blob/0a90bf8e6b90e9226f8c15b34eb7b1e4bf6d632e/android/src/main/java/com/google/samples/apps/iosched/util/PlayServicesUtils.java
public class PlayServicesUtils {
public static boolean checkGooglePlaySevices(final Activity activity) {
final int googlePlayServicesCheck = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
switch (googlePlayServicesCheck) {
case ConnectionResult.SUCCESS:
return true;
case ConnectionResult.SERVICE_DISABLED:
case ConnectionResult.SERVICE_INVALID:
case ConnectionResult.SERVICE_MISSING:
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(googlePlayServicesCheck, activity, 0);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialogInterface) {
activity.finish();
}
});
dialog.show();
}
return false;
}
}
Here is how to use it in an Activity: https://github.com/google/iosched/blob/cf1f30b4c752f275518384a9b71404ee501fc473/android/src/main/java/com/google/samples/apps/iosched/ui/BaseActivity.java
#Override
protected void onResume() {
super.onResume();
// Verifies the proper version of Google Play Services exists on the device.
PlayServicesUtils.checkGooglePlaySevices(this);
}
Does this mean that there is a new version of google play services in the play store?
From the site latest update was on December 2014
Does this mean that the app needs a newer version than the one that is currently installed in the device?
You can check if the device has the higher version ofGoogle Play Service than the one on your app like so:
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable( getApplicationContext() );
if(status == ConnectionResult.SUCCESS) {
//OK
}else if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){
Toast.makeText(context,"please udpate your google play service",Toast.LENGTH_SHORT).show
}
Documentation was updated, now it is clear:
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.
Note that all of the current answers reference GooglePlayServicesUtil which is now deprecated. See GooglePlayServicesUtil vs GoogleApiAvailability for details on how to perform the Google Play Services version compatibility check.
I resolved this problem by updating the last version of Youtube library available in : https://developers.google.com/youtube/android/player/downloads
In the gradle :
implementation files('libs/YouTubeAndroidPlayerApi.jar')

Categories

Resources