PlusClient | Android Developers

Quicknav Quicknav
Links

- Google Play Developer Console
- Android Developers Blog
- About Android

Android Sites

- Android.com
- Android Developers
- Android Open Source Project

Language
close

- - Get Started
  - Style
  - Patterns
  - Building Blocks
  - Downloads
  - Videos
- - Training
  - API Guides
  - Reference
  - Tools
    - Get the SDK
  - Google Services
- - Google Play
  - Publishing
  - Promoting
  - App Quality
  - Spotlight
  - Open Distribution

## Results

Loading...
Summary: Nested Classes | Constants | Methods | Inherited Methods | [Expand All]
public class

# PlusClient

extends Object
implements GooglePlayServicesClient

java.lang.Object
↳ | com.google.android.gms.plus.PlusClient

## Class Overview

The main entry point for Google+ integration. Use the PlusClient after the asynchronous `connect()` method has been called and your listener's `onConnected()` method is called.

When your app is done using PlusClient, call `disconnect()`, even if the async result from `connect()` has not yet been delivered.

You should instantiate this object in your Activity's `onCreate(Bundle)` method and then call `connect()` in `onStart()` and `disconnect()` in `onStop()`, regardless of the state.

## Summary

Nested Classes
class | PlusClient.Builder | Builder to create a `PlusClient` to connect to Google Play services.
interface | PlusClient.OnAccessRevokedListener | Listener interface for when the token revocation request has completed.
interface | PlusClient.OnMomentsLoadedListener | Listener interface for when moments are loaded.
interface | PlusClient.OnPeopleLoadedListener | Listener interface for when a collection of people are loaded.
interface | PlusClient.OnPersonLoadedListener | Listener interface for when a `Person` is loaded

Constants
String | DEFAULT_ACCOUNT | Specify `<>` as the account name to indicate that the default account on the device should be used.
String | KEY_REQUEST_VISIBLE_ACTIVITIES | Bundle key for specifying which user's app activity (moment) types can be written to Google.

Public Methods
void | clearDefaultAccount() Clears the default account for this `PlusClient` instance.
void | connect() Connects the client to Google Play services.
void | disconnect() Closes the connection to Google Play services.
String | getAccountName() Gets the account name resolved by Google Play services.
Person | getCurrentPerson() Returns profile information for the current user.
boolean | isConnected() Checks if the client is currently connected to the service, so that requests to other methods will succeed.
boolean | isConnectionCallbacksRegistered(GooglePlayServicesClient.ConnectionCallbacks listener) Returns true if the specified listener is currently registered to receive connection events.
boolean | isConnectionFailedListenerRegistered(GooglePlayServicesClient.OnConnectionFailedListener listener) Returns true if the specified listener is currently registered to receive connection failed events.
void | loadMoments(PlusClient.OnMomentsLoadedListener listener, int maxResults, String pageToken, Uri targetUrl, String type, String userId) List all of the moments for a particular user.
void | loadMoments(PlusClient.OnMomentsLoadedListener listener) List all of the moments for the currently signed-in user.
void | loadPeople(PlusClient.OnPeopleLoadedListener listener, int collection, int orderBy, int maxResults, String pageToken) Loads a list of people in the specified collection.
void | loadPeople(PlusClient.OnPeopleLoadedListener listener, int collection) Loads a list of people in the specified collection.
void | loadPerson(PlusClient.OnPersonLoadedListener listener, String userId) Loads profile information for a specified Google+ user.
void | registerConnectionCallbacks(GooglePlayServicesClient.ConnectionCallbacks listener) Registers a listener to receive connection events from this `GooglePlayServicesClient`.
void | registerConnectionFailedListener(GooglePlayServicesClient.OnConnectionFailedListener listener) Registers a listener to receive connection failed events from this `GooglePlayServicesClient`.
void | removeMoment(String momentId) Delete a moment.
void | revokeAccessAndDisconnect(PlusClient.OnAccessRevokedListener listener) Revokes access given to the current application.
void | unregisterConnectionCallbacks(GooglePlayServicesClient.ConnectionCallbacks listener) Removes a connection listener from this `GooglePlayServicesClient`.
void | unregisterConnectionFailedListener(GooglePlayServicesClient.OnConnectionFailedListener listener) Removes a connection failed listener from the `GooglePlayServicesClient`.
void | writeMoment(Moment moment) Writes a moment.

[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()
final void | wait(long arg0, int arg1)
final void | wait(long arg0)
From interface com.google.android.gms.common.GooglePlayServicesClient
abstract void | connect() Connects the client to Google Play services.
abstract void | disconnect() Closes the connection to Google Play services.
abstract boolean | isConnected() Checks if the client is currently connected to the service, so that requests to other methods will succeed.
abstract boolean | isConnectionCallbacksRegistered(GooglePlayServicesClient.ConnectionCallbacks listener) Returns true if the specified listener is currently registered to receive connection events.
abstract boolean | isConnectionFailedListenerRegistered(GooglePlayServicesClient.OnConnectionFailedListener listener) Returns true if the specified listener is currently registered to receive connection failed events.
abstract void | registerConnectionCallbacks(GooglePlayServicesClient.ConnectionCallbacks listener) Registers a listener to receive connection events from this `GooglePlayServicesClient`.
abstract void | registerConnectionFailedListener(GooglePlayServicesClient.OnConnectionFailedListener listener) Registers a listener to receive connection failed events from this `GooglePlayServicesClient`.
abstract void | unregisterConnectionCallbacks(GooglePlayServicesClient.ConnectionCallbacks listener) Removes a connection listener from this `GooglePlayServicesClient`.
abstract void | unregisterConnectionFailedListener(GooglePlayServicesClient.OnConnectionFailedListener listener) Removes a connection failed listener from the `GooglePlayServicesClient`.

## Constants

#### public static final String DEFAULT_ACCOUNT

Specify `<>` as the account name to indicate that the default account on the device should be used.

Constant Value: "<>"

#### public static final String KEY_REQUEST_VISIBLE_ACTIVITIES

Bundle key for specifying which user's app activity (moment) types can be written to Google. The list of activity types are represented as a space-separated string passed in the extras Bundle when calling `ERROR(/GoogleAuthUtil#getToken(Context, String, String, Bundle))`.

This bundle key should be included in the extras Bundle when calling `ERROR(/GoogleAuthUtil#getToken(Context, String, String, Bundle))` and should only be used when requesting the `PLUS_LOGIN` OAuth 2.0 scope. See Types of moments for the full list of valid activity types. Example usage:

         Bundle bundle = new Bundle();
         bundle.putString(PlusClient.KEY_REQUEST_VISIBLE_ACTIVITIES,
                  "http://schemas.google.com/AddActivity http://schemas.google.com/BuyActivity");
         String token = GoogleAuthUtil.getToken(context, accountName, Scopes.PLUS_LOGIN, bundle);

Constant Value: "request_visible_actions"

## Public Methods

#### public void clearDefaultAccount ()

Clears the default account for this `PlusClient` instance. Use this method to allow the user to choose a different account the next time `connect()` is called.

This operation doesn't affect existing connected instances of `PlusClient`. This client and any others will continue to use the same account. You should call `disconnect()` and `connect()` if you want to reset the account.

#### public 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()` is called. On a failure, `onConnectionFailed(ConnectionResult)` is called.

#### public void disconnect ()

Closes the connection to Google Play services. No calls can be made on this object after calling this method.

#### public String getAccountName ()

Gets the account name resolved by Google Play services. The permission `<uses-permission android:name="android.permission.GET_ACCOUNTS" />` must be declared in your `AndroidManifest.xml` to use this method.

##### Returns

- The account name. If the user has not selected an account, null is returned.

##### Throws

SecurityException | If your app doesn't have the `GET_ACCOUNTS` permission.

#### public Person getCurrentPerson ()

Returns profile information for the current user. For more information, see: https://developers.google.com/+/api/latest/people/get.

This method requires the OAuth 2.0 scope `PLUS_PROFILE` or `PLUS_LOGIN` specified in the `PlusClient` constructor.

This method can return `null` if the required scopes weren't specified in the `PlusClient` constructor, or if there was a network error while connecting.

##### Returns

- Profile information for the current user, if available, or `null` otherwise.

#### public 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 boolean isConnectionCallbacksRegistered (GooglePlayServicesClient.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.

#### public boolean isConnectionFailedListenerRegistered (GooglePlayServicesClient.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.

#### public void loadMoments (PlusClient.OnMomentsLoadedListener listener, int maxResults, String pageToken, Uri targetUrl, String type, String userId)

List all of the moments for a particular user. For more information, see https://developers.google.com/+/api/latest/moments/list.

This method requires the `PLUS_LOGIN` OAuth 2.0 scope specified in the `PlusClient` constructor.

##### Parameters

listener | The `PlusClient.OnMomentsLoadedListener` to receive a callback when the moments are loaded.
maxResults | The maximum number of moments to include in the response, which is used for paging. For any response, the actual number returned might be less than the specified maxResults.
pageToken | The continuation token, which is used to page through large result sets. To get the next page of results, set this parameter to the value of nextPageToken from the previous response.
targetUrl | Only moments containing this targetUrl will be returned.
type | Only moments of this type will be returned.
userId | The ID of the user to get moments for. The special value "me" can be used to indicate the authenticated user.

#### public void loadMoments (PlusClient.OnMomentsLoadedListener listener)

List all of the moments for the currently signed-in user. For more information, see https://developers.google.com/+/api/latest/moments/list.

This method requires the `PLUS_LOGIN` OAuth 2.0 scope specified in the `PlusClient` constructor.

##### Parameters

listener | The `PlusClient.OnMomentsLoadedListener` to receive a callback when the moments are loaded.

#### public void loadPeople (PlusClient.OnPeopleLoadedListener listener, int collection, int orderBy, int maxResults, String pageToken)

Loads a list of people in the specified collection. For more information, see: https://developers.google.com/+/api/latest/people/list.

Each `Person` will contain the `id`, `displayName`, `image`, `objectType`, and `url` fields populated. To retrieve additional profile data, use the `loadPerson(OnPersonLoadedListener, String)` method.

This method requires the `PLUS_LOGIN` OAuth 2.0 scope specified in the `PlusClient` constructor.

##### Parameters

listener | The `PlusClient.OnPeopleLoadedListener` to receive a callback when the people are loaded.
collection | The collection of people to list. Valid values include: `VISIBLE` - The list of people who this user has added to one or more circles.
orderBy | The order to return people in. Valid values are: `ALPHABETICAL` - Order the people by their display name. `BEST` - Order people based on the relevance to the viewer.
maxResults | The maximum number of people to include in the response, which is used for paging. For any response, the actual number returned might be less than the specified maxResults.
pageToken | The continuation token, which is used to page through large result sets. To get the next page of results, set this parameter to the value of nextPageToken from the previous response.

#### public void loadPeople (PlusClient.OnPeopleLoadedListener listener, int collection)

Loads a list of people in the specified collection. For more information, see: https://developers.google.com/+/api/latest/people/list.

Each `Person` will contain the `id`, `displayName`, `image`, `objectType`, and `url` fields populated. To retrieve additional profile data, use the `loadPerson(OnPersonLoadedListener, String)` method.

This method requires the `PLUS_LOGIN` OAuth 2.0 scope specified in the `PlusClient` constructor.

##### Parameters

listener | The `PlusClient.OnPeopleLoadedListener` to receive a callback when the people are loaded.
collection | The collection of people to list. Valid values include: `VISIBLE` - The list of people who this user has added to one or more circles.

#### public void loadPerson (PlusClient.OnPersonLoadedListener listener, String userId)

Loads profile information for a specified Google+ user. For more information, see: https://developers.google.com/+/api/latest/people/get.

This method requires the OAuth 2.0 scope `PLUS_PROFILE` or `PLUS_LOGIN` specified in the `PlusClient` constructor.

##### Parameters

listener | The `PlusClient.OnPersonLoadedListener` to receive a callback when the person is loaded.
userId | The ID of the person to get the profile for. The special value "me" can be used to indicate the authenticated user.

#### public void registerConnectionCallbacks (GooglePlayServicesClient.ConnectionCallbacks listener)

Registers a listener to receive connection events from this `GooglePlayServicesClient`. If we are already connected, the listener's `onConnected()` 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()` 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 void registerConnectionFailedListener (GooglePlayServicesClient.OnConnectionFailedListener listener)

Registers a listener to receive connection failed events from this `GooglePlayServicesClient`. Unlike `registerConnectionCallbacks(GooglePlayServicesClient.ConnectionCallbacks)`, if we are 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 void removeMoment (String momentId)

Delete a moment. For more information, see https://developers.google.com/+/api/latest/moments/remove.

This method requires the `PLUS_LOGIN` OAuth 2.0 scope specified in the `PlusClient` constructor.

##### Parameters

momentId | The ID of the moment to delete.

#### public void revokeAccessAndDisconnect (PlusClient.OnAccessRevokedListener listener)

Revokes access given to the current application. The current `PlusClient` will be disconnected when the `PlusClient.OnAccessRevokedListener` receives a callback.

##### Parameters

listener | The `PlusClient.OnAccessRevokedListener` to receive a callback when the token is revoked.

#### public void unregisterConnectionCallbacks (GooglePlayServicesClient.ConnectionCallbacks listener)

Removes a connection listener from this `GooglePlayServicesClient`. 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 void unregisterConnectionFailedListener (GooglePlayServicesClient.OnConnectionFailedListener listener)

Removes a connection failed listener from the `GooglePlayServicesClient`. 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.

#### public void writeMoment (Moment moment)

Writes a moment. For more information, see https://developers.google.com/+/api/latest/moments/insert.

This is a fire-and-forget method that writes the user's moment asynchronously. If there is a network error, Google Play services attempts to send the request again when the device comes back online. Requests can fail if there are problems with the account or format of specified in `moment`. To debug, run `adb logcat` in a terminal and find errors related to moments in the output.

This method requires the `PLUS_LOGIN` OAuth 2.0 scope specified in the `PlusClient` constructor.

##### Parameters

moment | The moment description.