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:
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
}
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"
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:
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.)