GoogleApiClient

abstract class GoogleApiClient


The main entry point for Google Play services integration.

GoogleApiClient is used with a variety of static methods. Some of these methods require that GoogleApiClient be connected, some will queue up calls before GoogleApiClient is connected; check the specific API documentation to determine whether you need to be connected.

Before any operation is executed, the GoogleApiClient must be connected. The simplest way to manage the connection is to use enableAutoManage. See Accessing Google APIs.

GoogleApiClient instances are not thread-safe. To access Google APIs from multiple threads simultaneously, create a GoogleApiClient on each thread. GoogleApiClient service connections are cached internally, so creating multiple instances is fast.

Summary

Nested types

This class is deprecated.

Use GoogleApi based APIs instead.

This interface is deprecated.

use GoogleApi based APIs instead. seemoving past googleapiclient.

This interface is deprecated.

use GoogleApi based apis instead. seemoving past googleapiclient. api implementors should use the stand-alone instead.

Constants

const Int

If authenticated APIs are present they will attempt to connect, but failure of an authenticated API will not cause the GoogleApiClient connection to fail.

const Int

If a required authenticated API fails to connect, the entire GoogleApiClient will fail to connect and a failed ConnectionResult will be delivered to OnConnectionFailedListener#onConnectionFailed.

Public constructors

Public functions

abstract ConnectionResult!

Connects the client to Google Play services.

abstract ConnectionResult!
blockingConnect(timeout: Long, unit: TimeUnit)

Connects the client to Google Play services.

abstract PendingResult<Status!>!

Clears the account selected by the user and reconnects the client asking the user to pick an account again if useDefaultAccount was set.

abstract Unit

Connects the client to Google Play services.

Unit
connect(signInMode: Int)

Connects the client to Google Play services using the given sign in mode.

abstract Unit

Closes the connection to Google Play services.

abstract Unit
dump(
    prefix: String!,
    fd: FileDescriptor!,
    writer: PrintWriter!,
    args: Array<String!>!
)

Prints the GoogleApiClient's state into the given stream.

java-static Unit
dumpAll(
    prefix: String!,
    fd: FileDescriptor!,
    writer: PrintWriter!,
    args: Array<String!>!
)

Prints the state of all GoogleApiClients in the current process into the given stream.

abstract ConnectionResult

Returns the ConnectionResult for the GoogleApiClient's connection to the specified API.

abstract Boolean

Returns whether or not this GoogleApiClient has the specified API in a connected state.

abstract Boolean

Checks if the client is currently connected to the service, so that requests to other methods will succeed.

abstract Boolean

Checks if the client is attempting to connect to the service.

abstract Boolean

Returns true if the specified listener is currently registered to receive connection events.

abstract Boolean

Returns true if the specified listener is currently registered to receive connection failed events.

abstract Unit

Closes the current connection to Google Play services and creates a new connection.

abstract Unit

Registers a listener to receive connection events from this GoogleApiClient.

abstract Unit

Registers a listener to receive connection failed events from this GoogleApiClient.

abstract Unit
stopAutoManage(lifecycleActivity: FragmentActivity)

Disconnects the client and stops automatic lifecycle management.

abstract Unit

Removes a connection listener from this GoogleApiClient.

abstract Unit

Removes a connection failed listener from the GoogleApiClient.

Constants

SIGN_IN_MODE_OPTIONAL

const val SIGN_IN_MODE_OPTIONAL = 2: Int

If authenticated APIs are present they will attempt to connect, but failure of an authenticated API will not cause the GoogleApiClient connection to fail. After ConnectionCallbacks#onConnected is received, the status of an authenticated API can be checked with hasConnectedApi.

A GoogleApiClient using this mode may be transitioned between authenticated and unauthenticated states by adding GOOGLE_SIGN_IN_API and using . To get an Intent that will allow the user to sign-in, call getSignInIntent. To sign the user out, call signOut.

It is an error to call connect with no arguments on a client in this mode.

See also
GoogleSignInApi

SIGN_IN_MODE_REQUIRED

const val SIGN_IN_MODE_REQUIRED = 1: Int

If a required authenticated API fails to connect, the entire GoogleApiClient will fail to connect and a failed ConnectionResult will be delivered to OnConnectionFailedListener#onConnectionFailed. Once a connection has successfully completed the only way to disconnect the authenticated APIs is to call disconnect on this GoogleApiClient.

Using this mode is equivalent to calling connect on a GoogleApiClient that contains authenticated APIs.

It is an error to use this mode if no authenticated APIs have been added to this GoogleApiClient.

Public constructors

GoogleApiClient

GoogleApiClient()

Public functions

blockingConnect

abstract fun blockingConnect(): ConnectionResult!

Connects the client to Google Play services. Blocks until the connection either succeeds or fails.

Keep in mind this method will cause ANRs if called from the main thread.

If the client is already connected, this methods returns immediately. If the client is already connecting (for example due to a prior call to connect), this method blocks until the existing connection attempt completes. If a prior connection attempt has already failed, then a new connection attempt is started.

Returns
ConnectionResult!

the result of the connection

blockingConnect

abstract fun blockingConnect(timeout: Long, unit: TimeUnit): ConnectionResult!

Connects the client to Google Play services. Blocks until the connection either succeeds or fails, or the timeout is reached.

Keep in mind this method will cause ANRs if called from the main thread.

If the client is already connected, this methods returns immediately. If the client is already connecting (for example due to a prior call to connect), this method blocks until the existing connection attempt completes or the timeout is reached. If a prior connection attempt has already failed, then a new connection attempt is started.

Parameters
timeout: Long

the maximum time to wait

unit: TimeUnit

the time unit of the timeout argument

Returns
ConnectionResult!

the result of the connection

clearDefaultAccountAndReconnect

abstract fun clearDefaultAccountAndReconnect(): PendingResult<Status!>!

Clears the account selected by the user and reconnects the client asking the user to pick an account again if useDefaultAccount was set.

Returns
PendingResult<Status!>!

the pending result is fired once the default account has been cleared, but before the client is reconnected - for that ConnectionCallbacks can be used.

connect

abstract fun connect(): Unit

Connects the client to Google Play services. This method returns immediately, and connects to the service in the background. If the connection is successful, ConnectionCallbacks#onConnected is called and enqueued items are executed. On a failure, OnConnectionFailedListener#onConnectionFailed is called.

If the client is already connected or connecting, this method does nothing.

connect

fun connect(signInMode: Int): Unit

Connects the client to Google Play services using the given sign in mode.

It is an error to make multiple calls to this method passing different modes. Once a mode is selected, all future connect calls must use the same mode.

disconnect

abstract fun disconnect(): Unit

Closes the connection to Google Play services. No calls can be made using this client after calling this method. Any method calls that haven't executed yet will be canceled, and their onResult callbacks won't be called.

If the connection to the remote service hasn't been established yet, all enqueued calls will be canceled.

See also
connect

dump

abstract fun dump(
    prefix: String!,
    fd: FileDescriptor!,
    writer: PrintWriter!,
    args: Array<String!>!
): Unit

Prints the GoogleApiClient's state into the given stream.

Parameters
prefix: String!

Desired prefix to prepend at each line of output.

fd: FileDescriptor!

The raw file descriptor that the dump is being sent to.

writer: PrintWriter!

The PrintWriter to use for writing the dump.

args: Array<String!>!

Additional arguments to the dump request.

dumpAll

java-static fun dumpAll(
    prefix: String!,
    fd: FileDescriptor!,
    writer: PrintWriter!,
    args: Array<String!>!
): Unit

Prints the state of all GoogleApiClients in the current process into the given stream.

This can be used to diagnose lifecycle issues where GoogleApiClients may be unintentionally left in the connected state. Note that the output may include clients that are no longer referenced but have not yet been garbage collected.

Parameters
prefix: String!

Desired prefix to prepend at each line of output.

fd: FileDescriptor!

The raw file descriptor that the dump is being sent to.

writer: PrintWriter!

The PrintWriter to use for writing the dump.

args: Array<String!>!

Additional arguments to the dump request.

getConnectionResult

abstract fun getConnectionResult(api: Api<Any!>): ConnectionResult

Returns the ConnectionResult for the GoogleApiClient's connection to the specified API. This method must only be called after connect has been called and before disconnect is called.

This method should be used to check the connection result of an API added via addApiIfAvailable in the event that the overall connection succeeded, but the individual API failed to connect. To check the failure of the overall connection, use addOnConnectionFailedListener.

This method may return stale results if the GoogleApiClient is reconnecting due to a lost network connection. It is guaranteed to return the most recent ConnectionResult from attempting to connect the given API, but will throw an IllegalStateException if called before calling connect, after calling disconnect, or after receiving OnConnectionFailedListener#onConnectionFailed. This method can be used to easily determine why an API failed to connect if it was not available. To determine whether a given API is currently connected (without potential stale results) see hasConnectedApi.

Parameters
api: Api<Any!>

The Api to retrieve the ConnectionResult of. Passing an API that was not registered with the GoogleApiClient results in an IllegalArgumentException.

hasConnectedApi

abstract fun hasConnectedApi(api: Api<Any!>): Boolean

Returns whether or not this GoogleApiClient has the specified API in a connected state.

Parameters
api: Api<Any!>

The Api to test the connection of.

isConnected

abstract fun isConnected(): Boolean

Checks if the client is currently connected to the service, so that requests to other methods will succeed. Applications should guard client actions caused by the user with a call to this method.

Returns
Boolean

true if the client is connected to the service.

isConnecting

abstract fun isConnecting(): Boolean

Checks if the client is attempting to connect to the service.

Returns
Boolean

true if the client is attempting to connect to the service.

isConnectionCallbacksRegistered

abstract fun isConnectionCallbacksRegistered(
    listener: GoogleApiClient.ConnectionCallbacks
): Boolean

Returns true if the specified listener is currently registered to receive connection events.

Parameters
listener: GoogleApiClient.ConnectionCallbacks

The listener to check for.

Returns
Boolean

true if the specified listener is currently registered to receive connection events.

isConnectionFailedListenerRegistered

abstract fun isConnectionFailedListenerRegistered(
    listener: GoogleApiClient.OnConnectionFailedListener
): Boolean

Returns true if the specified listener is currently registered to receive connection failed events.

Parameters
listener: GoogleApiClient.OnConnectionFailedListener

The listener to check for.

Returns
Boolean

true if the specified listener is currently registered to receive connection failed events.

reconnect

abstract fun reconnect(): Unit

Closes the current connection to Google Play services and creates a new connection. Equivalent to calling disconnect followed by connect.

This method closes the current connection then returns immediately and reconnects to the service in the background.

After calling this method, your application will receive ConnectionCallbacks#onConnected if the connection is successful, or OnConnectionFailedListener#onConnectionFailed if the connection failed.

See also
connect
disconnect

registerConnectionCallbacks

abstract fun registerConnectionCallbacks(
    listener: GoogleApiClient.ConnectionCallbacks
): Unit

Registers a listener to receive connection events from this GoogleApiClient. If the service is already connected, the listener's ConnectionCallbacks#onConnected method will be called immediately. Applications should balance calls to this method with calls to unregisterConnectionCallbacks to avoid leaking resources.

If the specified listener is already registered to receive connection events, this method will not add a duplicate entry for the same listener, but will still call the listener's ConnectionCallbacks#onConnected method if currently connected.

Note that the order of messages received here may not be stable, so clients should not rely on the order that multiple listeners receive events in.

Parameters
listener: GoogleApiClient.ConnectionCallbacks

the listener where the results of the asynchronous connect call are delivered.

registerConnectionFailedListener

abstract fun registerConnectionFailedListener(
    listener: GoogleApiClient.OnConnectionFailedListener
): Unit

Registers a listener to receive connection failed events from this GoogleApiClient. Unlike registerConnectionCallbacks, if the service is not already connected, the listener's OnConnectionFailedListener#onConnectionFailed method will not be called immediately. Applications should balance calls to this method with calls to unregisterConnectionFailedListener to avoid leaking resources.

If the specified listener is already registered to receive connection failed events, this method will not add a duplicate entry for the same listener.

Note that the order of messages received here may not be stable, so clients should not rely on the order that multiple listeners receive events in.

Parameters
listener: GoogleApiClient.OnConnectionFailedListener

the listener where the results of the asynchronous connect call are delivered.

stopAutoManage

abstract fun stopAutoManage(lifecycleActivity: FragmentActivity): Unit

Disconnects the client and stops automatic lifecycle management. Use this before creating a new client (which might be necessary when switching accounts, changing the set of used APIs etc.).

This method must be called from the main thread.

Parameters
lifecycleActivity: FragmentActivity

the activity managing the client's lifecycle.

Throws
java.lang.IllegalStateException

if called from outside of the main thread.

See also
enableAutoManage

unregisterConnectionCallbacks

abstract fun unregisterConnectionCallbacks(
    listener: GoogleApiClient.ConnectionCallbacks
): Unit

Removes a connection listener from this GoogleApiClient. Note that removing a listener does not generate any callbacks.

If the specified listener is not currently registered to receive connection events, this method will have no effect.

Parameters
listener: GoogleApiClient.ConnectionCallbacks

the listener to unregister.

unregisterConnectionFailedListener

abstract fun unregisterConnectionFailedListener(
    listener: GoogleApiClient.OnConnectionFailedListener
): Unit

Removes a connection failed listener from the GoogleApiClient. Note that removing a listener does not generate any callbacks.

If the specified listener is not currently registered to receive connection failed events, this method will have no effect.

Parameters
listener: GoogleApiClient.OnConnectionFailedListener

the listener to unregister.