GoogleApiClient.Builder

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(context: Context)

Builder to help construct the GoogleApiClient object.

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

Builder to help construct the GoogleApiClient object.

Public functions

GoogleApiClient.Builder!

Specify which Apis are requested by your app.

GoogleApiClient.Builder!
@CanIgnoreReturnValue
<O : Api.ApiOptions.HasOptions?> addApi(api: Api<O!>, options: O)

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 : Api.ApiOptions.HasOptions?> addApiIfAvailable(
    api: Api<O!>,
    options: O,
    scopes: Array<Scope!>!
)

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(
    fragmentActivity: FragmentActivity,
    unresolvedConnectionFailedListener: GoogleApiClient.OnConnectionFailedListener?
)

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

GoogleApiClient.Builder!
@CanIgnoreReturnValue
enableAutoManage(
    fragmentActivity: FragmentActivity,
    clientId: Int,
    unresolvedConnectionFailedListener: GoogleApiClient.OnConnectionFailedListener?
)

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

Builder(context: Context)

Builder to help construct the GoogleApiClient object.

Parameters
context: Context

The context to use for the connection.

Builder

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

Builder to help construct the GoogleApiClient object.

Parameters
context: Context

The context to use for the connection.

connectedListener: GoogleApiClient.ConnectionCallbacks

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

connectionFailedListener: GoogleApiClient.OnConnectionFailedListener

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

Public functions

addApi

@CanIgnoreReturnValue
fun addApi(api: Api<Api.ApiOptions.NotRequiredOptions!>): GoogleApiClient.Builder!

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

Parameters
api: Api<Api.ApiOptions.NotRequiredOptions!>

The Api requested by your app.

See also
Api

addApi

@CanIgnoreReturnValue
fun <O : Api.ApiOptions.HasOptions?> addApi(api: Api<O!>, options: O): GoogleApiClient.Builder!

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

Parameters
api: Api<O!>

The Api requested by your app.

options: O

Any additional parameters required for the specific AP

See also
Api

addApiIfAvailable

@CanIgnoreReturnValue
fun <T : Api.ApiOptions.NotRequiredOptions?> addApiIfAvailable(
    api: Api<Api.ApiOptions.NotRequiredOptions!>,
    scopes: Array<Scope!>!
): GoogleApiClient.Builder!

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
api: Api<Api.ApiOptions.NotRequiredOptions!>

The Api requested by your app.

scopes: Array<Scope!>!

Scopes required by this API.

See also
Api

addApiIfAvailable

@CanIgnoreReturnValue
fun <O : Api.ApiOptions.HasOptions?> addApiIfAvailable(
    api: Api<O!>,
    options: O,
    scopes: Array<Scope!>!
): GoogleApiClient.Builder!

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
api: Api<O!>

The Api requested by your app.

scopes: Array<Scope!>!

Scopes required by this API.

See also
Api

addConnectionCallbacks

@CanIgnoreReturnValue
fun addConnectionCallbacks(listener: GoogleApiClient.ConnectionCallbacks): GoogleApiClient.Builder!

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

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

addOnConnectionFailedListener

@CanIgnoreReturnValue
fun addOnConnectionFailedListener(
    listener: GoogleApiClient.OnConnectionFailedListener
): GoogleApiClient.Builder!

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
listener: GoogleApiClient.OnConnectionFailedListener

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

addScope

@CanIgnoreReturnValue
fun addScope(scope: Scope): GoogleApiClient.Builder!

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
scope: Scope

The OAuth 2.0 scopes requested by your app.

See also
Scopes

build

fun build(): GoogleApiClient!

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

Returns
GoogleApiClient!

The GoogleApiClient object.

enableAutoManage

@CanIgnoreReturnValue
fun enableAutoManage(
    fragmentActivity: FragmentActivity,
    unresolvedConnectionFailedListener: GoogleApiClient.OnConnectionFailedListener?
): GoogleApiClient.Builder!

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
fragmentActivity: FragmentActivity

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

unresolvedConnectionFailedListener: GoogleApiClient.OnConnectionFailedListener?

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
fun enableAutoManage(
    fragmentActivity: FragmentActivity,
    clientId: Int,
    unresolvedConnectionFailedListener: GoogleApiClient.OnConnectionFailedListener?
): GoogleApiClient.Builder!

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
fragmentActivity: FragmentActivity

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

clientId: Int

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.

unresolvedConnectionFailedListener: GoogleApiClient.OnConnectionFailedListener?

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

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

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

setGravityForPopups

@CanIgnoreReturnValue
fun setGravityForPopups(gravityForPopups: Int): 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.

Default value is TOP|CENTER_HORIZONTAL.

Parameters
gravityForPopups: Int

The gravity which controls the placement of games service popups.

setHandler

@CanIgnoreReturnValue
fun setHandler(handler: Handler): GoogleApiClient.Builder!

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
fun setViewForPopups(viewForPopups: View): GoogleApiClient.Builder!

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

Parameters
viewForPopups: View

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

useDefaultAccount

@CanIgnoreReturnValue
fun useDefaultAccount(): GoogleApiClient.Builder!

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