Skip to content
marchbold edited this page Aug 14, 2020 · 3 revisions

Google API Availability Helper

The GoogleApiAvailability class is a helper class for verifying that the Google Play services APK is available and up-to-date on this device.

This allows you to check and update the Play services on a user's device at an appropriate time in your application.

Our ANEs will automatically do this if you attempt to use functionality that requires an update to the Play services on a user device, however using this class will allow you to control the timing of this interaction so not to interupt your application.

Imports

You will need to add the following imports:

import com.distriqt.extension.playservices.base.ConnectionResult;
import com.distriqt.extension.playservices.base.GoogleApiAvailability;

Checking availability

Firstly to check Google Play Services are available you can use the isGooglePlayServicesAvailable method:

var result:int = GoogleApiAvailability.instance.isGooglePlayServicesAvailable();
if (result == ConnectionResult.SUCCESS)
{
    // Google Play Services are available and up-to-date
}

Resolution

If the result is anything but SUCCESS then Google Play services is not available currently on the users' device.

This could be for many reasons, for example, that Google Play services aren't installed, that they are out of date, or that the user's device doesn't support Google Play services.

Many of these are resolvable by the user, by getting them to update or install Google Play services.

You should next check if the error is "user resolvable" using the isUserRecoverableError method:

var result:int = GoogleApiAvailability.instance.isGooglePlayServicesAvailable();
if (result != ConnectionResult.SUCCESS)
{
    if (GoogleApiAvailability.instance.isUserRecoverableError( result ))
    {
        // The error can be resolved
    }
    else 
    {
        // This is an unresolvable error and Google Play Services functionality will not be available on this device 
    }
}

If the error can be resolved you have several options on getting your user to resolve the error.

  • Create your own UI using the error description (getErrorString(int)) and then call attemptResolution(int) to resolve the error:
GoogleApiAvailability.instance.attemptResolution( result );
  • Display a native dialog to redirect the user to the resolution:
GoogleApiAvailability.instance.showErrorDialog( result );
  • Display a native notification to redirect the user to the resolution:
GoogleApiAvailability.instance.showErrorNotification( result );

In our experience, the showErrorDialog() and showErrorNotification() calls can be unreliable. We suggest utilising the attemptResolution() call and your own UI.

Example

var result:int = GoogleApiAvailability.instance.isGooglePlayServicesAvailable();
if (result != ConnectionResult.SUCCESS)
{
    if (GoogleApiAvailability.instance.isUserRecoverableError( result ))
    {
        GoogleApiAvailability.instance.attemptResolution( result );
    }
    else
    {
        trace( "Google Play Services aren't available on this device" );
    }
}
else
{
    trace( "Google Play Services are Available" );
}

Version

You can retrieve the version of the Google Play Services installed on the device by calling the getInstalledGooglePlayServicesVersion() function.

var gpsVersion:Number = GoogleApiAvailability.instance.getInstalledGooglePlayServicesVersion();

This numeric value will correspond to the version of the Google Play Services package installed on the device, you can use this if you need to determine a minimum version for your application and react accordingly.