Summary: Nested Classes | Constants | Ctors | Methods | Inherited Methods | [Expand All]
public abstract class

# GoogleApiClient

extends Object

java.lang.Object
↳ | com.google.android.gms.common.api.GoogleApiClient

## Class Overview

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(FragmentActivity, GoogleApiClient.OnConnectionFailedListener)`. See Accessing Google APIs.

## Summary

Nested Classes
class | GoogleApiClient.Builder | Builder to configure a `GoogleApiClient`.
interface | GoogleApiClient.ConnectionCallbacks | Provides callbacks that are called when the client is connected or disconnected from the service.
interface | GoogleApiClient.OnConnectionFailedListener | Provides callbacks for scenarios that result in a failed attempt to connect the client to the service.

Constants
int | SIGN_IN_MODE_OPTIONAL | If authenticated APIs are present they will attempt to connect, but failure of an authenticated API will not cause the GoogleApiClient connection to fail.
int | SIGN_IN_MODE_REQUIRED | If a required authenticated API fails to connect, the entire GoogleApiClient will fail to connect and a failed `ConnectionResult` will be delivered to `onConnectionFailed(ConnectionResult)`.

Public Constructors
| GoogleApiClient()

Public Methods
abstract ConnectionResult | blockingConnect() Connects the client to Google Play services.
abstract ConnectionResult | blockingConnect(long timeout, TimeUnit unit) Connects the client to Google Play services.
abstract PendingResult<Status> | clearDefaultAccountAndReconnect() Clears the account selected by the user and reconnects the client asking the user to pick an account again if `useDefaultAccount()` was set.
void | connect(int signInMode) Connects the client to Google Play services using the given sign in mode.
abstract void | connect() Connects the client to Google Play services.
abstract void | disconnect() Closes the connection to Google Play services.
abstract void | dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) Prints the GoogleApiClient's state into the given stream.
static void | dumpAll(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) Prints the state of all GoogleApiClients in the current process into the given stream.
abstract ConnectionResult | getConnectionResult(Api<?> api) Returns the `ConnectionResult` for the GoogleApiClient's connection to the specified API.
abstract boolean | hasConnectedApi(Api<?> api) Returns whether or not this GoogleApiClient has the specified API in a connected state.
abstract boolean | isConnected() Checks if the client is currently connected to the service, so that requests to other methods will succeed.
abstract boolean | isConnecting() Checks if the client is attempting to connect to the service.
abstract boolean | isConnectionCallbacksRegistered(GoogleApiClient.ConnectionCallbacks listener) Returns true if the specified listener is currently registered to receive connection events.
abstract boolean | isConnectionFailedListenerRegistered(GoogleApiClient.OnConnectionFailedListener listener) Returns true if the specified listener is currently registered to receive connection failed events.
abstract void | reconnect() Closes the current connection to Google Play services and creates a new connection.
abstract void | registerConnectionCallbacks(GoogleApiClient.ConnectionCallbacks listener) Registers a listener to receive connection events from this `GoogleApiClient`.
abstract void | registerConnectionFailedListener(GoogleApiClient.OnConnectionFailedListener listener) Registers a listener to receive connection failed events from this `GoogleApiClient`.
abstract void | stopAutoManage(FragmentActivity lifecycleActivity) Disconnects the client and stops automatic lifecycle management.
abstract void | unregisterConnectionCallbacks(GoogleApiClient.ConnectionCallbacks listener) Removes a connection listener from this `GoogleApiClient`.
abstract void | unregisterConnectionFailedListener(GoogleApiClient.OnConnectionFailedListener listener) Removes a connection failed listener from the `GoogleApiClient`.

[Expand] Inherited Methods
From class java.lang.Object
Object | clone()
boolean | equals(Object arg0)
void | finalize()
final Class<?> | getClass()
int | hashCode()
final void | notify()
final void | notifyAll()
String | toString()
final void | wait(long arg0, int arg1)
final void | wait(long arg0)
final void | wait()

## Constants

#### public static final int SIGN_IN_MODE_OPTIONAL

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 `onConnected(Bundle)` is received, the status of an authenticated API can be checked with `hasConnectedApi(Api)`.

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

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

##### See Also

- `GoogleSignInApi`

Constant Value: 2 (0x00000002)

#### public static final int SIGN_IN_MODE_REQUIRED

If a required authenticated API fails to connect, the entire GoogleApiClient will fail to connect and a failed `ConnectionResult` will be delivered to `onConnectionFailed(ConnectionResult)`. 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.

Constant Value: 1 (0x00000001)

## Public Constructors

#### public GoogleApiClient ()

## Public Methods

#### public abstract ConnectionResult blockingConnect ()

Connects the client to Google Play services. Blocks until the connection either succeeds or fails. This is not allowed on the UI 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

- the result of the connection

#### public abstract ConnectionResult blockingConnect (long timeout, TimeUnit unit)

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

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 | the maximum time to wait
unit | the time unit of the `timeout` argument

##### Returns

- the result of the connection

#### public abstract PendingResult<Status> clearDefaultAccountAndReconnect ()

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

##### Returns

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

#### public void connect (int signInMode)

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.

##### See Also

- `connect()`
- `SIGN_IN_MODE_REQUIRED`
- `SIGN_IN_MODE_OPTIONAL`

#### public abstract void connect ()

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

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

#### public abstract void disconnect ()

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(Result)` 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()`

#### public abstract void dump (String prefix, FileDescriptor fd, PrintWriter writer, String[] args)

Prints the GoogleApiClient's state into the given stream.

##### Parameters

prefix | Desired prefix to prepend at each line of output.
fd | The raw file descriptor that the dump is being sent to.
writer | The PrintWriter to use for writing the dump.
args | Additional arguments to the dump request.

#### public static void dumpAll (String prefix, FileDescriptor fd, PrintWriter writer, String[] args)

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 | Desired prefix to prepend at each line of output.
fd | The raw file descriptor that the dump is being sent to.
writer | The PrintWriter to use for writing the dump.
args | Additional arguments to the dump request.

#### public abstract ConnectionResult getConnectionResult (Api<?> api)

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 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 `onConnectionFailed(ConnectionResult)`. 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(Api)`.

##### Parameters

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

#### public abstract boolean hasConnectedApi (Api<?> api)

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

##### Parameters

api | The `Api` to test the connection of.

#### public abstract boolean isConnected ()

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

- true if the client is connected to the service.

#### public abstract boolean isConnecting ()

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

##### Returns

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

#### public abstract boolean isConnectionCallbacksRegistered (GoogleApiClient.ConnectionCallbacks listener)

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

##### Parameters

listener | The listener to check for.

##### Returns

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

##### See Also

- `registerConnectionCallbacks(ConnectionCallbacks)`
- `unregisterConnectionCallbacks(ConnectionCallbacks)`

#### public abstract boolean isConnectionFailedListenerRegistered (GoogleApiClient.OnConnectionFailedListener listener)

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

##### Parameters

listener | The listener to check for.

##### Returns

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

##### See Also

- `registerConnectionFailedListener(OnConnectionFailedListener)`
- `unregisterConnectionFailedListener(OnConnectionFailedListener)`

#### public abstract void reconnect ()

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 `onConnected(Bundle)` if the connection is successful, or `onConnectionFailed(ConnectionResult)` if the connection failed.

##### See Also

- `connect()`
- `disconnect()`

#### public abstract void registerConnectionCallbacks (GoogleApiClient.ConnectionCallbacks listener)

Registers a listener to receive connection events from this `GoogleApiClient`. If the service is already connected, the listener's `onConnected(Bundle)` method will be called immediately. Applications should balance calls to this method with calls to `unregisterConnectionCallbacks(ConnectionCallbacks)` 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 `onConnected(Bundle)` 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 | the listener where the results of the asynchronous `connect()` call are delivered.

#### public abstract void registerConnectionFailedListener (GoogleApiClient.OnConnectionFailedListener listener)

Registers a listener to receive connection failed events from this `GoogleApiClient`. Unlike `registerConnectionCallbacks(GoogleApiClient.ConnectionCallbacks)`, if the service is not already connected, the listener's `onConnectionFailed(ConnectionResult)` method will not be called immediately. Applications should balance calls to this method with calls to `unregisterConnectionFailedListener(OnConnectionFailedListener)` 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 | the listener where the results of the asynchronous `connect()` call are delivered.

#### public abstract void stopAutoManage (FragmentActivity lifecycleActivity)

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 | the activity managing the client's lifecycle.

##### Throws

IllegalStateException | if called from outside of the main thread.

##### See Also

- `enableAutoManage(FragmentActivity, int, OnConnectionFailedListener)`

#### public abstract void unregisterConnectionCallbacks (GoogleApiClient.ConnectionCallbacks listener)

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 | the listener to unregister.

#### public abstract void unregisterConnectionFailedListener (GoogleApiClient.OnConnectionFailedListener listener)

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 | the listener to unregister.