How do you prompt the user to update google play services? - android

I can easily detect that my device's google play services app needs to be updated, and present the getErrorDialogFragment() to prompt the user to update it with:
GoogleApiAvailability googleApi = GoogleApiAvailability.getInstance();
mServiceAvailabilityCode = googleApi.isGooglePlayServicesAvailable(this);
if (mServiceAvailabilityCode == ConnectionResult.SUCCESS) {
...
else {
if (googleApi.isUserResolvableError(mServiceAvailabilityCode)) {
switch (mServiceAvailabilityCode) {
....
case SERVICE_VERSION_UPDATE_REQUIRED:
googleApi.showErrorDialogFragment(SplashActivity.this, mServiceAvailabilityCode, PLAY_SERVICES_RESOLUTION_REQUEST);
break;
....
}
However, if Google play services is disabled AND out of date, then, the user is presented with a dialog with an "Update" button, once the user presses it, the app returns immediately to the onActivityResult, which I then catch the response and request code in OnActivityResult like this:
#Override
protected void onActivityResult(int requestCode, int mServiceAvailabilityCode, Intent data) {
super.onActivityResult(requestCode, mServiceAvailabilityCode, data);
switch (requestCode) {
case PLAY_SERVICES_RESOLUTION_REQUEST:
finish();
// do i need to launch app manager-> app info for google play services ?
So, by the user pressing the "Update" button on the dialog did not launch Android's Playstore and load the "Playstor services" app for the user to update, which I had expected it to do. Pressing "Update" just goes straight back to onActivityResult. This is where I'm confused. Shouldn't Android have launched this for me ? Or do I have to do it myself in OnActivityResult ?

The problem is caused by a specific condition when you have 2 issues with Google Play Services (GPS). Because GPS is also disabled Google Play Store (GPT), will not run on the device.
If your Google Play Services is out of date, then calling showErrorDialogFragment using an error code of ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED, (error code 2), which works fine.
But if your GPS is both disabled AND out-of-date, then there is an issue with the way googleApiAvailability api works.If you call isGooglePlayServicesAvailable() it will return the first error it finds, but it's not necessarily the error you want to resolve first. The problem is knowing that you have another error you need to address first. isGooglePlayServicesAvailable() does not help in this regard.
In my case play services is both disabled, AND out of date. So, the approach is to first call showErrorDialogFragment and you'll get a response error code for SERVICE_VERSION_UPDATE_REQUIRED.
Android will attempt to resolve it by sending a pendingIntent to launch Google Play Store (GPT) to update GPS, but this will fail, as GPT depends on an ENABLED version of GPS. Because you're calling showErrorDialogFragment it will call onActivityResult after it fails to launch GPT.
The next step is codig the onActivityResult. I needed to test for isGooglePlayServicesAvailable() again. If you still get the same error code (SERVICE_VERSION_UPDATE_REQUIRED), then you need to call showErrorDialogFragment again in onActivityResult, but this time pass it a different error code, ConnectionResult.SERVICE_DISABLED (error code 3). This will take the user to the app manager to ENABLE google play services first. Then when returning to the app, you need to test for isGooglePlayServicesAvailable and it should then detect google services is still out of date. If you successfully update the app, onActivityResult should allow you to determine that isGooglePlayServicesAvailable is succcessful and you can continue. Note that you will may need to add a flag that so that you know to test again for google play services compatibility rather than continue executing a startup process.
(So, really what googleApiAvailability should do is return the disabled error first (ie ConnectionResult.SERVICE_DISABLED aka error code 3), so you can resolve that first before attempting to update GPS.)

This is working for me
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int result = googleAPI.isGooglePlayServicesAvailable(this);
if(result != ConnectionResult.SUCCESS) {
if(googleAPI.isUserResolvableError(result)) {
//prompt the dialog to update google play
googleAPI.getErrorDialog(this,result,PLAY_SERVICES_RESOLUTION_REQUEST).show();
}
}
else{
//google play up to date
}

Not sure in which version Google has it fixed, but with the latest 11.6.0 you can get the correct statusCode and the dialog that leads to Settings to enable the Google Play Services.
Logcat:
11-24 05:48:07.266 V/FA: Activity resumed, time: 2070779368
11-24 05:48:07.320 W/FA: Service connection failed: ConnectionResult{statusCode=SERVICE_DISABLED, resolution=null, message=null}
Dialog:

Google Play Services are available on Play Store to download and update. You can simply open Play Store.
Here is link to the services.
What you can do is start activity with ACTION_VIEW intent like below
String LINK_TO_GOOGLE_PLAY_SERVICES = "play.google.com/store/apps/details?id=com.google.android.gms&hl=en";
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://" + LINK_TO_GOOGLE_PLAY_SERVICES)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://" + LINK_TO_GOOGLE_PLAY_SERVICES)));
}

There is also this code:
https://gist.github.com/kristopherjohnson/7554793
Which require:
implementation "com.google.android.gms:play-services-auth:$playServiceAuthVersion"

Related

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)?

How to check if user has installed/updated Google Play services?

My Android app requires Google Play services to display Google Maps. I've included a function call that checks for the availability of Google Play services and proceed if it exists. If it doesn't exist or has an outdated version, an error dialog is shown that redirects user to Play Store. I wanted to ask, how can I check if a user has actually installed (or updated) Google Play services when they land back to my app? I'm using Fragment and checking for the availability of Google Play services in onResume().
The function is as follow:
private boolean isPlayServicesConfigured() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity().getApplicationContext());
if(status == ConnectionResult.SUCCESS)
return true;
else {
Log.d("STATUS", "Error connecting with Google Play services. Code: " + String.valueOf(status));
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getActivity(), status);
dialog.show();
return false;
}
}
Moreover, even if I get the status of Google Play Services back in my app, how do I reload the fragment? Can I call onCreateView() programatically?

"this app won\'t work unless you update google play services" error

I am trying to implement the google play services sign-in to my app.
I added libraries (Google-play-services lib, basegameutils lib).I have added the sign in button. The app is not crashing. Everything that I have done seems to be ok.
I signed the app with the keytool and export it, then install it to my phone. I added the SHA1 code to my test app on developers console. And added My app Id to my app.
But when I tap on the sign in button it say with a pop-up "this app won\'t work unless you update google play services". When tap on the "update" it seems the service is up to date.
Is this a problem with my device? Or else What can I try to solve that?
My device is Samsung Galaxy S3.
Update
I added this code :
private boolean checkIfMapsIsOk() {
int checkGooglePlayServices = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices, this, 1122).show();
Toast.makeText(getApplicationContext(), "err"+checkGooglePlayServices, Toast.LENGTH_SHORT).show();
return false;
} else {
Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
return true;
}
}
and the toast result is "err2". And I looked up for the error numbers from http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html
It says on that site "public static final int SERVICE_VERSION_UPDATE_REQUIRED"
"The installed version of Google Play services is out of date. The calling activity should pass this error code to getErrorDialog(int, Activity, int) to get a localized error dialog that will resolve the error when shown.
Constant Value: 2 (0x00000002)"
So from this I think the reason for error is my device. But my device sees no updates. It is up to date. This is so strange.

Google Play Games Services achievement activity closing immediately

I'm updating my game to the new Google Play Serviced library, leaderboards and achievements were already working flawlessly, but now when I try to open the achievement activity, it immediately closes without showing any exception in logcat.
I'm logged in with my google account, which is correctly configured as a test user.
public void gameServicesGetAchievements() {
if (gameHelper == null)
return;
Intent i = Games.Achievements.getAchievementsIntent(gameHelper.getApiClient());
((Activity) ctx).startActivityForResult(i, REQUEST_CODE_ACHIEVEMENTS);
}
The activity opens, but closes immediately before showing the achievements list. This is what I get in logcat
D/GameHelper(30459): GameHelper: onActivityResult: req=9802, resp=RESULT_RECONNECT_REQUIRED
D/GameHelper(30459): GameHelper: onActivityResult: request code not meant for us. Ignoring.
I tried handling the RESULT_RECONNECT_REQUIRED code in my onActivityResult, but nothing changes.
#Override
public void onActivityResult(int request, int response, Intent data) {
super.onActivityResult(request, response, data);
GameHelper helper = resolver.getGameServicesHelper();
if (helper != null) {
helper.onActivityResult(request, response, data);
}
if (response == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED) {
helper.disconnect();
}
}
The leaderboards activities start correctly and work flawlessly.
Found the problem... it wasn't a problem in my app, just the Google Play Games app on my device had some corrupted data or something.
I followed the steps here and that fixed it.
http://howlukeseesit.blogspot.it/2014/08/fixing-google-play-games.html
First navigate to settings > apps and find and tap on Google play
games.
Next tap uninstall updates, then tap clear data.
Next go back
to the apps section and find Google Play Services, tap that then tap
Manage space and then tap on clear all data.
Now all you have to do
is go into the Play store and manually update Google Play Games.

Ask for Google Maps Version in my app

I have an app that uses Google Maps. Since, with the new Api in Android, the map wont work if the user don't have the Google Maps app up to date, is there any way that my app can know the version that the user have installed? And... can my app know wich version is the last in Google Play?
the map wont work if the user don't have the Google Maps app up to date, is there any way that my app can know the version that the user have installed?
Your app does not care about the version of Google Maps. Your app cares about having the Play Services Framework installed.
My sample apps, such as this one, use an AbstractMapActivity that wraps up the details of checking for the Play Services Framework, in a call to readyToGo():
protected boolean readyToGo() {
int status=
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status == ConnectionResult.SUCCESS) {
return(true);
}
else if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
ErrorDialogFragment.newInstance(status)
.show(getSupportFragmentManager(),
TAG_ERROR_DIALOG_FRAGMENT);
}
else {
Toast.makeText(this, R.string.no_maps, Toast.LENGTH_LONG).show();
finish();
}
return(false);
}
This will return true if isGooglePlayServicesAvailable() itself returns true. Otherwise, if possible, it will display a dialog (there's an ErrorDialogFragment inner class) that will lead the user to install the Play Services Framework. Or, it will display a Toast for an unrecoverable problem (though production code should use something else, like a crouton).
GooglePlayServicesUtil has its own set of JavaDocs.

Categories

Resources