GoogleApiAvailability

public class GoogleApiAvailability extends Object

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

Constant Summary

String GOOGLE_PLAY_SERVICES_PACKAGE Package name for Google Play services.

Field Summary

public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE Google Play services client library version (declared in library's AndroidManifest.xml android:versionCode).

Public Method Summary

Task<Void>
checkApiAvailability(HasApiKey<?> api, HasApiKey...<?> apis)
Checks the availability of the specified APIs.
Task<Void>
checkApiAvailability(GoogleApi<?> api, GoogleApi...<?> apis)
Checks the availability of the specified APIs.
Dialog
getErrorDialog(Activity activity, int errorCode, int requestCode)
Returns a dialog to address the provided errorCode.
Dialog
getErrorDialog(Fragment fragment, int errorCode, int requestCode, DialogInterface.OnCancelListener cancelListener)
Returns a dialog to address the provided errorCode.
Dialog
getErrorDialog(Activity activity, int errorCode, int requestCode, DialogInterface.OnCancelListener cancelListener)
Returns a dialog to address the provided errorCode.
Dialog
getErrorDialog(Fragment fragment, int errorCode, int requestCode)
Returns a dialog to address the provided errorCode.
PendingIntent
getErrorResolutionPendingIntent(Context context, ConnectionResult result)
Returns a PendingIntent to address the provided connection failure.
PendingIntent
getErrorResolutionPendingIntent(Context context, int errorCode, int requestCode)
Returns a PendingIntent to address the provided errorCode.
final String
getErrorString(int errorCode)
Returns a human-readable string of the error code returned from isGooglePlayServicesAvailable(Context).
static GoogleApiAvailability
getInstance()
Returns the singleton instance of GoogleApiAvailability.
int
isGooglePlayServicesAvailable(Context context)
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.
int
isGooglePlayServicesAvailable(Context context, int minApkVersion)
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 or the version is not older than the one specified in minApkVersion.
final boolean
isUserResolvableError(int errorCode)
Determines whether an error can be resolved via user action.
Task<Void>
makeGooglePlayServicesAvailable(Activity activity)
Attempts to make Google Play services available on this device.
void
setDefaultNotificationChannelId(Context context, String notificationChannelId)
Overrides the default notification channel for Google Play services availability notifications.
boolean
showErrorDialogFragment(Activity activity, int errorCode, int requestCode)
Displays a DialogFragment for an error code returned by isGooglePlayServicesAvailable(Context).
boolean
showErrorDialogFragment(Activity activity, int errorCode, int requestCode, DialogInterface.OnCancelListener cancelListener)
Displays a DialogFragment for an error code returned by isGooglePlayServicesAvailable(Context).
boolean
showErrorDialogFragment(Activity activity, int errorCode, ActivityResultLauncher<IntentSenderRequest> activityResultLauncher, DialogInterface.OnCancelListener cancelListener)
Displays a DialogFragment for an errorCode returned by isGooglePlayServicesAvailable(Context).
void
showErrorNotification(Context context, int errorCode)
Displays a notification for an error code returned from isGooglePlayServicesAvailable(Context), if it is resolvable by the user.
void
showErrorNotification(Context context, ConnectionResult result)
Displays a notification for a connection failure, if it is resolvable by the user.

Inherited Method Summary

Constants

public static final String GOOGLE_PLAY_SERVICES_PACKAGE

Package name for Google Play services.

Constant Value: "com.google.android.gms"

Fields

public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE

Google Play services client library version (declared in library's AndroidManifest.xml android:versionCode).

Public Methods

public Task<Void> checkApiAvailability (HasApiKey<?> api, HasApiKey...<?> apis)

Checks the availability of the specified APIs.

If at least one of the APIs is unavailable, the task will fail with a AvailabilityException, which can be queried for individual API availability.

public Task<Void> checkApiAvailability (GoogleApi<?> api, GoogleApi...<?> apis)

Checks the availability of the specified APIs.

If at least one of the APIs is unavailable, the task will fail with a AvailabilityException, which can be queried for individual API availability.

public Dialog getErrorDialog (Activity activity, int errorCode, int requestCode)

Returns a dialog to address the provided errorCode. The returned dialog displays a localized message about the error and upon user confirmation (by tapping on dialog) will direct them to the Play Store if Google Play services is out of date or missing, or to system settings if Google Play services is disabled on the device. This method follows the Activity.startActivityForResult(Intent, int) and Activity.onActivityResult(int, int, Intent) API pattern. Consider migrating to showErrorDialogFragment(Activity, int, ActivityResultLauncher, OnCancelListener) as the ActivityResultContracts API is the recommended approach.

Parameters
activity parent activity for creating the dialog, also used for identifying language to display dialog in.
errorCode error code returned by isGooglePlayServicesAvailable(Context) call. If errorCode is ConnectionResult.SUCCESS then null is returned.
requestCode The number given when calling startActivityForResult.

public Dialog getErrorDialog (Fragment fragment, int errorCode, int requestCode, DialogInterface.OnCancelListener cancelListener)

Returns a dialog to address the provided errorCode. The returned dialog displays a localized message about the error and upon user confirmation (by tapping on dialog) will direct them to the Play Store if Google Play services is out of date or missing, or to system settings if Google Play services is disabled on the device. This method follows the Activity.startActivityForResult(Intent, int) and Activity.onActivityResult(int, int, Intent) API pattern. Consider migrating to showErrorDialogFragment(Activity, int, ActivityResultLauncher, OnCancelListener) as the ActivityResultContracts API is the recommended approach.

Parameters
fragment parent fragment for creating the dialog, also used for identifying language to display dialog in.
errorCode error code returned by isGooglePlayServicesAvailable(Context) call. If errorCode is ConnectionResult.SUCCESS then null is returned.
requestCode The number given when calling startActivityForResult.
cancelListener The DialogInterface.OnCancelListener to invoke if the dialog is canceled.

public Dialog getErrorDialog (Activity activity, int errorCode, int requestCode, DialogInterface.OnCancelListener cancelListener)

Returns a dialog to address the provided errorCode. The returned dialog displays a localized message about the error and upon user confirmation (by tapping on dialog) will direct them to the Play Store if Google Play services is out of date or missing, or to system settings if Google Play services is disabled on the device. This method follows the Activity.startActivityForResult(Intent, int) and Activity.onActivityResult(int, int, Intent) API pattern. Consider migrating to showErrorDialogFragment(Activity, int, ActivityResultLauncher, OnCancelListener) as the ActivityResultContracts API is the recommended approach.

Parameters
activity parent activity for creating the dialog, also used for identifying language to display dialog in.
errorCode error code returned by isGooglePlayServicesAvailable(Context) call. If errorCode is ConnectionResult.SUCCESS then null is returned.
requestCode The number given when calling startActivityForResult.
cancelListener The DialogInterface.OnCancelListener to invoke if the dialog is canceled.

public Dialog getErrorDialog (Fragment fragment, int errorCode, int requestCode)

Returns a dialog to address the provided errorCode. The returned dialog displays a localized message about the error and upon user confirmation (by tapping on dialog) will direct them to the Play Store if Google Play services is out of date or missing, or to system settings if Google Play services is disabled on the device. This method follows the Activity.startActivityForResult(Intent, int) and Activity.onActivityResult(int, int, Intent) API pattern. Consider migrating to showErrorDialogFragment(Activity, int, ActivityResultLauncher, OnCancelListener) as the ActivityResultContracts API is the recommended approach.

Parameters
fragment parent fragment for creating the dialog, also used for identifying language to display dialog in.
errorCode error code returned by isGooglePlayServicesAvailable(Context) call. If errorCode is ConnectionResult.SUCCESS then null is returned.
requestCode The number given when calling startActivityForResult.

public PendingIntent getErrorResolutionPendingIntent (Context context, ConnectionResult result)

Returns a PendingIntent to address the provided connection failure.

If ConnectionResult.hasResolution() is true, then ConnectionResult.getResolution() will be returned. Otherwise, the returned PendingIntent will direct the user to either the Play Store if Google Play services is out of date or missing, or system settings if Google Play services is disabled on the device.

Parameters
context parent context for creating the PendingIntent.
result the connection failure. If successful or the error is not resolvable by the user, null is returned.

public PendingIntent getErrorResolutionPendingIntent (Context context, int errorCode, int requestCode)

Returns a PendingIntent to address the provided errorCode. It will direct the user to either the Play Store if Google Play services is out of date or missing, or system settings if Google Play services is disabled on the device.

Parameters
context parent context for creating the PendingIntent.
errorCode error code returned by isGooglePlayServicesAvailable(Context) call. If errorCode is ConnectionResult.SUCCESS then null is returned.
requestCode The requestCode given when calling startActivityForResult.

public final String getErrorString (int errorCode)

Returns a human-readable string of the error code returned from isGooglePlayServicesAvailable(Context).

public static GoogleApiAvailability getInstance ()

Returns the singleton instance of GoogleApiAvailability.

public int isGooglePlayServicesAvailable (Context context)

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.

Returns
  • status code indicating whether there was an error. Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_UPDATING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID

public int isGooglePlayServicesAvailable (Context context, int minApkVersion)

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 or the version is not older than the one specified in minApkVersion.

Returns
  • status code indicating whether there was an error. Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_UPDATING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID

public final boolean isUserResolvableError (int errorCode)

Determines whether an error can be resolved via user action. If true, proceed by calling GoogleApiAvailability.getErrorDialog(Activity, int, int) and showing the dialog.

Parameters
errorCode error code returned by isGooglePlayServicesAvailable(Context), or returned to your application via #onConnectionFailed(ConnectionResult)
Returns

public Task<Void> makeGooglePlayServicesAvailable (Activity activity)

Attempts to make Google Play services available on this device. If Play Services is already available, the returned Task may complete immediately.

If it is necessary to display UI in order to complete this request (e.g. sending the user to the Google Play store) the passed Activity will be used to display this UI.

It is recommended to call this method from Activity.onCreate(Bundle). If the passed Activity completes before the returned Task completes, the Task will fail with a CancellationException.

This method must be called from the main thread.

Returns
  • A Task. If this Task completes without throwing an exception, Play Services is available on this device.

public void setDefaultNotificationChannelId (Context context, String notificationChannelId)

Overrides the default notification channel for Google Play services availability notifications.

It is required to register a NotificationChannel with the notificationChannelId in NotificationManager before calling this method. If the NotificationChannel is not registered AND the platform is at least Android O, then a NullPointerException will be thrown.

Parameters
context The calling context for setting the notification channel.
notificationChannelId The notification channel for Google Play services availability notifications

public boolean showErrorDialogFragment (Activity activity, int errorCode, int requestCode)

Displays a DialogFragment for an error code returned by isGooglePlayServicesAvailable(Context). This method follows the Activity.startActivityForResult(Intent, int) and Activity.onActivityResult(int, int, Intent) API pattern. Consider migrating to showErrorDialogFragment(Activity, int, ActivityResultLauncher, OnCancelListener) as the ActivityResultContracts API is the recommended approach.

Parameters
activity parent activity for creating the dialog, also used for identifying language to display dialog in.
errorCode error code returned by isGooglePlayServicesAvailable(Context) call. If errorCode is ConnectionResult.SUCCESS then this does nothing.
requestCode The number given when calling startActivityForResult.
Returns
  • true if the dialog is shown, false otherwise

public boolean showErrorDialogFragment (Activity activity, int errorCode, int requestCode, DialogInterface.OnCancelListener cancelListener)

Displays a DialogFragment for an error code returned by isGooglePlayServicesAvailable(Context). This method follows the Activity.startActivityForResult(Intent, int) and Activity.onActivityResult(int, int, Intent) API pattern. Consider migrating to showErrorDialogFragment(Activity, int, ActivityResultLauncher, OnCancelListener) as the ActivityResultContracts API is the recommended approach.

Parameters
activity parent activity for creating the dialog, also used for identifying language to display dialog in.
errorCode error code returned by isGooglePlayServicesAvailable(Context) call. If errorCode is ConnectionResult.SUCCESS then this does nothing
requestCode The number given when calling startActivityForResult.
cancelListener The DialogInterface.OnCancelListener to invoke if the dialog is canceled.
Returns
  • true if the dialog is shown, false otherwise.

public boolean showErrorDialogFragment (Activity activity, int errorCode, ActivityResultLauncher<IntentSenderRequest> activityResultLauncher, DialogInterface.OnCancelListener cancelListener)

Displays a DialogFragment for an errorCode returned by isGooglePlayServicesAvailable(Context).

Parameters
activity parent activity for creating the dialog, also used for identifying language to display dialog in.
errorCode error code returned by isGooglePlayServicesAvailable(Context) call. If errorCode is ConnectionResult.SUCCESS then this does nothing.
activityResultLauncher The ActivityResultLauncher to invoke if the user opts to attempt to remediate the error described by the dialog.
cancelListener The DialogInterface.OnCancelListener to invoke if the dialog is canceled.
Returns
  • true if the dialog is shown, false otherwise.

public void showErrorNotification (Context context, int errorCode)

Displays a notification for an error code returned from isGooglePlayServicesAvailable(Context), if it is resolvable by the user.

This method is similar to getErrorDialog(Activity, int, int), but is provided for background tasks that cannot or should not display dialogs.

Parameters
context The calling context for displaying the notification.
errorCode Error code returned by isGooglePlayServicesAvailable(Context). For other values, including ConnectionResult.SUCCESS, no notification is shown.

public void showErrorNotification (Context context, ConnectionResult result)

Displays a notification for a connection failure, if it is resolvable by the user.

Parameters
context The calling context used to display the notification.
result The connection failure. If successful or the error is not resolvable by the user, no notification is shown.