GoogleApiAvailability

class GoogleApiAvailability


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

Summary

Constants

const String
GOOGLE_PLAY_SERVICES_PACKAGE = "com.google.android.gms"

Package name for Google Play services.

const Int

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

Public functions

Task<Void!>

Checks the availability of the specified APIs.

Task<Void!>

Checks the availability of the specified APIs.

Dialog?
getErrorDialog(activity: Activity!, errorCode: Int, requestCode: Int)

Returns a dialog to address the provided errorCode.

Dialog?
getErrorDialog(fragment: Fragment!, errorCode: Int, requestCode: Int)

Returns a dialog to address the provided errorCode.

Dialog?
getErrorDialog(
    activity: Activity!,
    errorCode: Int,
    requestCode: Int,
    cancelListener: DialogInterface.OnCancelListener?
)

Returns a dialog to address the provided errorCode.

Dialog?
getErrorDialog(
    fragment: Fragment!,
    errorCode: Int,
    requestCode: Int,
    cancelListener: DialogInterface.OnCancelListener?
)

Returns a dialog to address the provided errorCode.

PendingIntent?
getErrorResolutionPendingIntent(
    context: Context!,
    result: ConnectionResult!
)

Returns a PendingIntent to address the provided connection failure.

PendingIntent?
getErrorResolutionPendingIntent(
    context: Context!,
    errorCode: Int,
    requestCode: Int
)

Returns a PendingIntent to address the provided errorCode.

String
getErrorString(errorCode: Int)

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

java-static GoogleApiAvailability

Returns the singleton instance of GoogleApiAvailability.

Int

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.

Boolean

Determines whether an error can be resolved via user action.

Task<Void!>

Attempts to make Google Play services available on this device.

Unit
@TargetApi(value = Build.VERSION_CODES.O)
setDefaultNotificationChannelId(
    context: Context,
    notificationChannelId: String
)

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

Boolean
showErrorDialogFragment(
    activity: Activity!,
    errorCode: Int,
    requestCode: Int
)

Displays a DialogFragment for an error code returned by isGooglePlayServicesAvailable.

Boolean
showErrorDialogFragment(
    activity: Activity!,
    errorCode: Int,
    activityResultLauncher: ActivityResultLauncher<IntentSenderRequest!>!,
    cancelListener: DialogInterface.OnCancelListener?
)

Displays a DialogFragment for an errorCode returned by isGooglePlayServicesAvailable.

Boolean
showErrorDialogFragment(
    activity: Activity!,
    errorCode: Int,
    requestCode: Int,
    cancelListener: DialogInterface.OnCancelListener?
)

Displays a DialogFragment for an error code returned by isGooglePlayServicesAvailable.

Unit
showErrorNotification(context: Context!, errorCode: Int)

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

Unit

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

Constants

GOOGLE_PLAY_SERVICES_PACKAGE

const val GOOGLE_PLAY_SERVICES_PACKAGE = "com.google.android.gms": String

Package name for Google Play services.

GOOGLE_PLAY_SERVICES_VERSION_CODE

const val GOOGLE_PLAY_SERVICES_VERSION_CODEInt

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

Public functions

checkApiAvailability

fun checkApiAvailability(api: GoogleApi<Any!>!, apis: Array<GoogleApi<Any!>!>!): Task<Void!>

Checks the availability of the specified APIs.

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

checkApiAvailability

fun checkApiAvailability(api: HasApiKey<Any!>!, apis: Array<HasApiKey<Any!>!>!): Task<Void!>

Checks the availability of the specified APIs.

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

getErrorDialog

fun getErrorDialog(activity: Activity!, errorCode: Int, requestCode: Int): Dialog?

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 startActivityForResult and onActivityResult API pattern. Consider migrating to showErrorDialogFragment as the androidx.activity.result.contract.ActivityResultContracts API is the recommended approach.

Parameters
activity: Activity!

parent activity for creating the dialog, also used for identifying language to display dialog in.

errorCode: Int

error code returned by isGooglePlayServicesAvailable call. If errorCode is SUCCESS then null is returned.

requestCode: Int

The number given when calling startActivityForResult.

getErrorDialog

fun getErrorDialog(fragment: Fragment!, errorCode: Int, requestCode: Int): Dialog?

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 startActivityForResult and onActivityResult API pattern. Consider migrating to showErrorDialogFragment as the androidx.activity.result.contract.ActivityResultContracts API is the recommended approach.

Parameters
fragment: Fragment!

parent fragment for creating the dialog, also used for identifying language to display dialog in.

errorCode: Int

error code returned by isGooglePlayServicesAvailable call. If errorCode is SUCCESS then null is returned.

requestCode: Int

The number given when calling startActivityForResult.

getErrorDialog

fun getErrorDialog(
    activity: Activity!,
    errorCode: Int,
    requestCode: Int,
    cancelListener: DialogInterface.OnCancelListener?
): Dialog?

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 startActivityForResult and onActivityResult API pattern. Consider migrating to showErrorDialogFragment as the androidx.activity.result.contract.ActivityResultContracts API is the recommended approach.

Parameters
activity: Activity!

parent activity for creating the dialog, also used for identifying language to display dialog in.

errorCode: Int

error code returned by isGooglePlayServicesAvailable call. If errorCode is SUCCESS then null is returned.

requestCode: Int

The number given when calling startActivityForResult.

cancelListener: DialogInterface.OnCancelListener?

The android.content.DialogInterface.OnCancelListener to invoke if the dialog is canceled.

getErrorDialog

fun getErrorDialog(
    fragment: Fragment!,
    errorCode: Int,
    requestCode: Int,
    cancelListener: DialogInterface.OnCancelListener?
): Dialog?

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 startActivityForResult and onActivityResult API pattern. Consider migrating to showErrorDialogFragment as the androidx.activity.result.contract.ActivityResultContracts API is the recommended approach.

Parameters
fragment: Fragment!

parent fragment for creating the dialog, also used for identifying language to display dialog in.

errorCode: Int

error code returned by isGooglePlayServicesAvailable call. If errorCode is SUCCESS then null is returned.

requestCode: Int

The number given when calling startActivityForResult.

cancelListener: DialogInterface.OnCancelListener?

The android.content.DialogInterface.OnCancelListener to invoke if the dialog is canceled.

getErrorResolutionPendingIntent

fun getErrorResolutionPendingIntent(
    context: Context!,
    result: ConnectionResult!
): PendingIntent?

Returns a PendingIntent to address the provided connection failure.

If hasResolution is true, then 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: Context!

parent context for creating the PendingIntent.

result: ConnectionResult!

the connection failure. If successful or the error is not resolvable by the user, null is returned.

getErrorResolutionPendingIntent

fun getErrorResolutionPendingIntent(
    context: Context!,
    errorCode: Int,
    requestCode: Int
): PendingIntent?

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: Context!

parent context for creating the PendingIntent.

errorCode: Int

error code returned by isGooglePlayServicesAvailable call. If errorCode is SUCCESS then null is returned.

requestCode: Int

The requestCode given when calling startActivityForResult.

getErrorString

fun getErrorString(errorCode: Int): String

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

getInstance

java-static fun getInstance(): GoogleApiAvailability

Returns the singleton instance of GoogleApiAvailability.

isGooglePlayServicesAvailable

fun isGooglePlayServicesAvailable(context: Context!): Int

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
Int

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

isUserResolvableError

fun isUserResolvableError(errorCode: Int): Boolean

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

Parameters
errorCode: Int

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

Returns
Boolean

true if the error is resolvable with getErrorDialog

makeGooglePlayServicesAvailable

@MainThread
fun makeGooglePlayServicesAvailable(activity: Activity!): Task<Void!>

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 onCreate. If the passed Activity completes before the returned Task completes, the Task will fail with a java.util.concurrent.CancellationException.

This method must be called from the main thread.

Returns
Task<Void!>

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

setDefaultNotificationChannelId

@TargetApi(value = Build.VERSION_CODES.O)
fun setDefaultNotificationChannelId(
    context: Context,
    notificationChannelId: String
): Unit

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 is not registered AND the platform is at least Android O, then a will be thrown.

Parameters
context: Context

The calling context for setting the notification channel.

notificationChannelId: String

The notification channel for Google Play services availability notifications

showErrorDialogFragment

fun showErrorDialogFragment(
    activity: Activity!,
    errorCode: Int,
    requestCode: Int
): Boolean

Displays a DialogFragment for an error code returned by isGooglePlayServicesAvailable. This method follows the startActivityForResult and onActivityResult API pattern. Consider migrating to showErrorDialogFragment as the androidx.activity.result.contract.ActivityResultContracts API is the recommended approach.

Parameters
activity: Activity!

parent activity for creating the dialog, also used for identifying language to display dialog in.

errorCode: Int

error code returned by isGooglePlayServicesAvailable call. If errorCode is SUCCESS then this does nothing.

requestCode: Int

The number given when calling startActivityForResult.

Returns
Boolean

true if the dialog is shown, false otherwise

showErrorDialogFragment

fun showErrorDialogFragment(
    activity: Activity!,
    errorCode: Int,
    activityResultLauncher: ActivityResultLauncher<IntentSenderRequest!>!,
    cancelListener: DialogInterface.OnCancelListener?
): Boolean

Displays a DialogFragment for an errorCode returned by isGooglePlayServicesAvailable.

Parameters
activity: Activity!

parent activity for creating the dialog, also used for identifying language to display dialog in.

errorCode: Int

error code returned by isGooglePlayServicesAvailable call. If errorCode is SUCCESS then this does nothing.

activityResultLauncher: ActivityResultLauncher<IntentSenderRequest!>!

The androidx.activity.result.ActivityResultLauncher to invoke if the user opts to attempt to remediate the error described by the dialog.

cancelListener: DialogInterface.OnCancelListener?

The android.content.DialogInterface.OnCancelListener to invoke if the dialog is canceled.

Returns
Boolean

true if the dialog is shown, false otherwise.

showErrorDialogFragment

fun showErrorDialogFragment(
    activity: Activity!,
    errorCode: Int,
    requestCode: Int,
    cancelListener: DialogInterface.OnCancelListener?
): Boolean

Displays a DialogFragment for an error code returned by isGooglePlayServicesAvailable. This method follows the startActivityForResult and onActivityResult API pattern. Consider migrating to showErrorDialogFragment as the androidx.activity.result.contract.ActivityResultContracts API is the recommended approach.

Parameters
activity: Activity!

parent activity for creating the dialog, also used for identifying language to display dialog in.

errorCode: Int

error code returned by isGooglePlayServicesAvailable call. If errorCode is SUCCESS then this does nothing

requestCode: Int

The number given when calling startActivityForResult.

cancelListener: DialogInterface.OnCancelListener?

The android.content.DialogInterface.OnCancelListener to invoke if the dialog is canceled.

Returns
Boolean

true if the dialog is shown, false otherwise.

showErrorNotification

fun showErrorNotification(context: Context!, errorCode: Int): Unit

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

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

Parameters
context: Context!

The calling context for displaying the notification.

errorCode: Int

Error code returned by isGooglePlayServicesAvailable. For other values, including SUCCESS, no notification is shown.

showErrorNotification

fun showErrorNotification(context: Context!, result: ConnectionResult!): Unit

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

Parameters
context: Context!

The calling context used to display the notification.

result: ConnectionResult!

The connection failure. If successful or the error is not resolvable by the user, no notification is shown.