GoogleApiClient.Builder

public final classGoogleApiClient.Builder

Builder to configure aGoogleApiClient.

Example:

GoogleApiClientclient=newGoogleApiClient.Builder(this)
.enableAutoManage(this/* FragmentActivity */,
this/* OnConnectionFailedListener */)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.setAccountName("users.account.name@gmail.com")
.build();
See also
<a href= "https://developers.google /android/guides/api-client" >Accessing Google

APIs

Summary

Public constructors

Builder to help construct theGoogleApiClientobject.

Builder to help construct theGoogleApiClientobject.

Public methods

GoogleApiClient.Builder

Specify which Apis are requested by your app.

GoogleApiClient.Builder

Specify which Apis are requested by your app.

GoogleApiClient.Builder

Specify which Apis should attempt to connect, but are not strictly required for your app.

GoogleApiClient.Builder

Specify which Apis should attempt to connect, but are not strictly required for your app.

GoogleApiClient.Builder

Registers a listener to receive connection events from thisGoogleApiClient.

GoogleApiClient.Builder

Adds a listener to register to receive connection failed events from this GoogleApiClient.

GoogleApiClient.Builder

Specify the OAuth 2.0 scopes requested by your app.

GoogleApiClient

Builds a newGoogleApiClientobject for communicating with the Google APIs.

GoogleApiClient.Builder

Enables automatic lifecycle management in a support libraryFragmentActivitythat connects the client inonStartand disconnects it inonStop.

GoogleApiClient.Builder

Enables automatic lifecycle management in a support libraryFragmentActivitythat connects the client inonStartand disconnects it inonStop.

GoogleApiClient.Builder

Specify an account name on the device that should be used.

GoogleApiClient.Builder

Specifies the part of the screen at which games service popups (for example, "welcome back" or "achievement unlocked" popups) will be displayed using gravity.

GoogleApiClient.Builder

Sets aHandlerto indicate which thread to use when invoking callbacks.

GoogleApiClient.Builder

Sets theViewto use as a content view for popups.

GoogleApiClient.Builder

Specify that the default account should be used when connecting to services.

Public constructors

Builder

publicBuilder(@NonNullContextcontext)

Builder to help construct theGoogleApiClientobject.

Parameters
@NonNullContextcontext

The context to use for the connection.

Builder

publicBuilder(
@NonNullContextcontext,
@NonNullGoogleApiClient.ConnectionCallbacksconnectedListener,
@NonNullGoogleApiClient.OnConnectionFailedListenerconnectionFailedListener
)

Builder to help construct theGoogleApiClientobject.

Parameters
@NonNullContextcontext

The context to use for the connection.

@NonNullGoogleApiClient.ConnectionCallbacksconnectedListener

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

@NonNullGoogleApiClient.OnConnectionFailedListenerconnectionFailedListener

The listener which will be notified if the connection attempt fails.

Public methods

addApi

@CanIgnoreReturnValue
publicGoogleApiClient.BuilderaddApi(@NonNullApi<Api.ApiOptions.NotRequiredOptions> api)

Specify which Apis are requested by your app. SeeApifor more information.

Parameters
@NonNullApi<Api.ApiOptions.NotRequiredOptions> api

The Api requested by your app.

See also
Api

addApi

@CanIgnoreReturnValue
publicGoogleApiClient.Builder<O extendsApi.ApiOptions.HasOptions>addApi(@NonNullApi<O> api, @NonNullO options)

Specify which Apis are requested by your app. SeeApifor more information.

Parameters
@NonNullApi<O> api

The Api requested by your app.

@NonNullO options

Any additional parameters required for the specific AP

See also
Api

addApiIfAvailable

@CanIgnoreReturnValue
publicGoogleApiClient.Builder<T extendsApi.ApiOptions.NotRequiredOptions>addApiIfAvailable(
@NonNullApi<Api.ApiOptions.NotRequiredOptions> api,
Scope[] scopes
)

Specify which Apis should attempt to connect, but are not strictly required for your app. The GoogleApiClient will try to connect to these Apis, but will not necessarily fail if there are only errors when connecting to an unavailable Api added with this method. SeeApifor more information.

Parameters
@NonNullApi<Api.ApiOptions.NotRequiredOptions> api

The Api requested by your app.

Scope[] scopes

Scopes required by this API.

See also
Api

addApiIfAvailable

@CanIgnoreReturnValue
publicGoogleApiClient.Builder<O extendsApi.ApiOptions.HasOptions>addApiIfAvailable(
@NonNullApi<O> api,
@NonNullO options,
Scope[] scopes
)

Specify which Apis should attempt to connect, but are not strictly required for your app. The GoogleApiClient will try to connect to these Apis, but will not necessarily fail if there are only errors when connecting to an unavailable Api added with this method. SeeApifor more information.

Parameters
@NonNullApi<O> api

The Api requested by your app.

Scope[] scopes

Scopes required by this API.

See also
Api

addConnectionCallbacks

@CanIgnoreReturnValue
publicGoogleApiClient.BuilderaddConnectionCallbacks(
@NonNullGoogleApiClient.ConnectionCallbackslistener
)

Registers a listener to receive connection events from thisGoogleApiClient.Applications should balance calls to this method with calls tounregisterConnectionCallbacksto 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.

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
@NonNullGoogleApiClient.ConnectionCallbackslistener

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

addOnConnectionFailedListener

@CanIgnoreReturnValue
publicGoogleApiClient.BuilderaddOnConnectionFailedListener(
@NonNullGoogleApiClient.OnConnectionFailedListenerlistener
)

Adds a listener to register to receive connection failed events from this GoogleApiClient.Applications should balance calls to this method with calls tounregisterConnectionFailedListenerto 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
@NonNullGoogleApiClient.OnConnectionFailedListenerlistener

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

addScope

@CanIgnoreReturnValue
publicGoogleApiClient.BuilderaddScope(@NonNullScopescope)

Specify the OAuth 2.0 scopes requested by your app. SeeScopesfor more information.

It is an error to call this method when using GOOGLE_SIGN_IN_API. UserequestScopesinstead.

Parameters
@NonNullScopescope

The OAuth 2.0 scopes requested by your app.

See also
Scopes

build

publicGoogleApiClientbuild()

Builds a newGoogleApiClientobject for communicating with the Google APIs.

Returns
GoogleApiClient

TheGoogleApiClientobject.

enableAutoManage

@CanIgnoreReturnValue
publicGoogleApiClient.BuilderenableAutoManage(
@NonNullFragmentActivityfragmentActivity,
@NullableGoogleApiClient.OnConnectionFailedListenerunresolvedConnectionFailedListener
)

Enables automatic lifecycle management in a support libraryFragmentActivitythat connects the client inonStartand disconnects it inonStop.

It handles user recoverable errors appropriately and calls OnConnectionFailedListener#onConnectionFailedon the unresolvedConnectionFailedListener if theConnectionResulthas no resolution. This eliminates most of the boiler plate associated with usingGoogleApiClient.

This method can only be used if this GoogleApiClient will be the only auto-managed client in the containing activity. The api client will be assigned a default client id.

When using this option,buildmust be called from the main thread.

Parameters
@NonNullFragmentActivityfragmentActivity

The activity that uses theGoogleApiClient.For lifecycle management to work correctly the activity must call its parent'sonActivityResult.

@NullableGoogleApiClient.OnConnectionFailedListenerunresolvedConnectionFailedListener

Called if the connection failed and there was no resolution or the user chose not to complete the provided resolution. If this listener is called, the client will no longer be auto-managed, and a new instance must be built. In the event that the user chooses not to complete a resolution, the will have a status code ofCANCELED.

Throws
java.lang.NullPointerException

if fragmentActivity is null

java.lang.IllegalStateException

if another GoogleApiClient is already being auto-managed with the default clientId.

See also
enableAutoManage

enableAutoManage

@CanIgnoreReturnValue
publicGoogleApiClient.BuilderenableAutoManage(
@NonNullFragmentActivityfragmentActivity,
int clientId,
@NullableGoogleApiClient.OnConnectionFailedListenerunresolvedConnectionFailedListener
)

Enables automatic lifecycle management in a support libraryFragmentActivitythat connects the client inonStartand disconnects it inonStop.

It handles user recoverable errors appropriately and calls {@param * unresolvedConnectionFailedListener} if theConnectionResulthas no resolution. This eliminates most of the boiler plate associated with usingGoogleApiClient.

When using this option,buildmust be called from the main thread.

Parameters
@NonNullFragmentActivityfragmentActivity

The activity that uses theGoogleApiClient.For lifecycle management to work correctly the activity must call its parent'sonActivityResult.

int clientId

A non-negative identifier for this client. At any given time, only one auto-managed client is allowed per id. To reuse an id you must first callstopAutoManageon the previous client.

@NullableGoogleApiClient.OnConnectionFailedListenerunresolvedConnectionFailedListener

Called if the connection failed and there was no resolution or the user chose not to complete the provided resolution. If this listener is called, the client will no longer be auto-managed, and a new instance must be built. In the event that the user chooses not to complete a resolution, the will have a status code ofCANCELED.

Throws
java.lang.NullPointerException

if fragmentActivity is null

java.lang.IllegalArgumentException

if clientId is negative.

java.lang.IllegalStateException

if clientId is already being auto-managed.

setAccountName

@CanIgnoreReturnValue
publicGoogleApiClient.BuildersetAccountName(StringaccountName)

Specify an account name on the device that should be used. If this is never called, the client will use the current default account for Google Play services for this application.

It is an error to call this method when using GOOGLE_SIGN_IN_API. Use #setAccountName(String) instead.

Parameters
StringaccountName

The account name on the device that should be used by.

setGravityForPopups

@CanIgnoreReturnValue
publicGoogleApiClient.BuildersetGravityForPopups(int gravityForPopups)

Specifies the part of the screen at which games service popups (for example, "welcome back" or "achievement unlocked" popups) will be displayed using gravity.

Default value isTOP|CENTER_HORIZONTAL.

Parameters
int gravityForPopups

The gravity which controls the placement of games service popups.

setHandler

@CanIgnoreReturnValue
publicGoogleApiClient.BuildersetHandler(@NonNullHandlerhandler)

Sets aHandlerto indicate which thread to use when invoking callbacks. Will not be used directly to handle callbacks. If this is not called then the application's main thread will be used.

setViewForPopups

@CanIgnoreReturnValue
publicGoogleApiClient.BuildersetViewForPopups(@NonNullViewviewForPopups)

Sets theViewto use as a content view for popups.

Parameters
@NonNullViewviewForPopups

The view to use as a content view for popups. View cannot be null.

useDefaultAccount

@CanIgnoreReturnValue
publicGoogleApiClient.BuilderuseDefaultAccount()

Specify that the default account should be used when connecting to services.