GoogleApiClient.Builder

public final class GoogleApiClient.Builder


Builder to configure a GoogleApiClient.

Example:

GoogleApiClient client = new GoogleApiClient.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.com/android/guides/api-client">Accessing Google

APIs

Summary

Public constructors

Builder to help construct the GoogleApiClient object.

Builder(
    @NonNull Context context,
    @NonNull GoogleApiClient.ConnectionCallbacks connectedListener,
    @NonNull GoogleApiClient.OnConnectionFailedListener connectionFailedListener
)

Builder to help construct the GoogleApiClient object.

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
@CanIgnoreReturnValue
<O extends Api.ApiOptions.HasOptions> addApiIfAvailable(
    @NonNull Api<O> api,
    @NonNull O options,
    Scope[] scopes
)

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 this GoogleApiClient.

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 new GoogleApiClient object for communicating with the Google APIs.

GoogleApiClient.Builder
@CanIgnoreReturnValue
enableAutoManage(
    @NonNull FragmentActivity fragmentActivity,
    @Nullable GoogleApiClient.OnConnectionFailedListener unresolvedConnectionFailedListener
)

Enables automatic lifecycle management in a support library FragmentActivity that connects the client in onStart and disconnects it in onStop.

GoogleApiClient.Builder
@CanIgnoreReturnValue
enableAutoManage(
    @NonNull FragmentActivity fragmentActivity,
    int clientId,
    @Nullable GoogleApiClient.OnConnectionFailedListener unresolvedConnectionFailedListener
)

Enables automatic lifecycle management in a support library FragmentActivity that connects the client in onStart and disconnects it in onStop.

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 a Handler to indicate which thread to use when invoking callbacks.

GoogleApiClient.Builder

Sets the View to use as a content view for popups.

GoogleApiClient.Builder

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

Public constructors

Builder

public Builder(@NonNull Context context)

Builder to help construct the GoogleApiClient object.

Parameters
@NonNull Context context

The context to use for the connection.

Builder

public Builder(
    @NonNull Context context,
    @NonNull GoogleApiClient.ConnectionCallbacks connectedListener,
    @NonNull GoogleApiClient.OnConnectionFailedListener connectionFailedListener
)

Builder to help construct the GoogleApiClient object.

Parameters
@NonNull Context context

The context to use for the connection.

@NonNull GoogleApiClient.ConnectionCallbacks connectedListener

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

@NonNull GoogleApiClient.OnConnectionFailedListener connectionFailedListener

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

Public methods

addApi

@CanIgnoreReturnValue
public GoogleApiClient.Builder addApi(@NonNull Api<Api.ApiOptions.NotRequiredOptions> api)

Specify which Apis are requested by your app. See Api for more information.

Parameters
@NonNull Api<Api.ApiOptions.NotRequiredOptions> api

The Api requested by your app.

See also
Api

addApi

@CanIgnoreReturnValue
public GoogleApiClient.Builder <O extends Api.ApiOptions.HasOptions> addApi(@NonNull Api<O> api, @NonNull O options)

Specify which Apis are requested by your app. See Api for more information.

Parameters
@NonNull Api<O> api

The Api requested by your app.

@NonNull O options

Any additional parameters required for the specific AP

See also
Api

addApiIfAvailable

@CanIgnoreReturnValue
public GoogleApiClient.Builder <T extends Api.ApiOptions.NotRequiredOptions> addApiIfAvailable(
    @NonNull Api<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. See Api for more information.

Parameters
@NonNull Api<Api.ApiOptions.NotRequiredOptions> api

The Api requested by your app.

Scope[] scopes

Scopes required by this API.

See also
Api

addApiIfAvailable

@CanIgnoreReturnValue
public GoogleApiClient.Builder <O extends Api.ApiOptions.HasOptions> addApiIfAvailable(
    @NonNull Api<O> api,
    @NonNull O 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. See Api for more information.

Parameters
@NonNull Api<O> api

The Api requested by your app.

Scope[] scopes

Scopes required by this API.

See also
Api

addConnectionCallbacks

@CanIgnoreReturnValue
public GoogleApiClient.Builder addConnectionCallbacks(
    @NonNull GoogleApiClient.ConnectionCallbacks listener
)

Registers a listener to receive connection events from this GoogleApiClient. 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.

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
@NonNull GoogleApiClient.ConnectionCallbacks listener

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

addOnConnectionFailedListener

@CanIgnoreReturnValue
public GoogleApiClient.Builder addOnConnectionFailedListener(
    @NonNull GoogleApiClient.OnConnectionFailedListener listener
)

Adds a listener to register to receive connection failed events from this GoogleApiClient. 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
@NonNull GoogleApiClient.OnConnectionFailedListener listener

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

addScope

@CanIgnoreReturnValue
public GoogleApiClient.Builder addScope(@NonNull Scope scope)

Specify the OAuth 2.0 scopes requested by your app. See Scopes for more information.

It is an error to call this method when using GOOGLE_SIGN_IN_API. Use requestScopes instead.

Parameters
@NonNull Scope scope

The OAuth 2.0 scopes requested by your app.

See also
Scopes

build

public GoogleApiClient build()

Builds a new GoogleApiClient object for communicating with the Google APIs.

Returns
GoogleApiClient

The GoogleApiClient object.

enableAutoManage

@CanIgnoreReturnValue
public GoogleApiClient.Builder enableAutoManage(
    @NonNull FragmentActivity fragmentActivity,
    @Nullable GoogleApiClient.OnConnectionFailedListener unresolvedConnectionFailedListener
)

Enables automatic lifecycle management in a support library FragmentActivity that connects the client in onStart and disconnects it in onStop.

It handles user recoverable errors appropriately and calls OnConnectionFailedListener#onConnectionFailed on the unresolvedConnectionFailedListener if the ConnectionResult has no resolution. This eliminates most of the boiler plate associated with using GoogleApiClient.

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, build must be called from the main thread.

Parameters
@NonNull FragmentActivity fragmentActivity

The activity that uses the GoogleApiClient. For lifecycle management to work correctly the activity must call its parent's onActivityResult.

@Nullable GoogleApiClient.OnConnectionFailedListener unresolvedConnectionFailedListener

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 of CANCELED.

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
public GoogleApiClient.Builder enableAutoManage(
    @NonNull FragmentActivity fragmentActivity,
    int clientId,
    @Nullable GoogleApiClient.OnConnectionFailedListener unresolvedConnectionFailedListener
)

Enables automatic lifecycle management in a support library FragmentActivity that connects the client in onStart and disconnects it in onStop.

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

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

Parameters
@NonNull FragmentActivity fragmentActivity

The activity that uses the GoogleApiClient. For lifecycle management to work correctly the activity must call its parent's onActivityResult.

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 call stopAutoManage on the previous client.

@Nullable GoogleApiClient.OnConnectionFailedListener unresolvedConnectionFailedListener

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 of CANCELED.

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
public GoogleApiClient.Builder setAccountName(String accountName)

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
String accountName

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

setGravityForPopups

@CanIgnoreReturnValue
public GoogleApiClient.Builder setGravityForPopups(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 is TOP|CENTER_HORIZONTAL.

Parameters
int gravityForPopups

The gravity which controls the placement of games service popups.

setHandler

@CanIgnoreReturnValue
public GoogleApiClient.Builder setHandler(@NonNull Handler handler)

Sets a Handler to 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
public GoogleApiClient.Builder setViewForPopups(@NonNull View viewForPopups)

Sets the View to use as a content view for popups.

Parameters
@NonNull View viewForPopups

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

useDefaultAccount

@CanIgnoreReturnValue
public GoogleApiClient.Builder useDefaultAccount()

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