Tip:
Highlight text to annotate it
X
KATHERINE KUAN: Hello.
My name is Katherine Kuan, and I'm a developer advocate
on the Developer Relations team here at Google.
Today, I'll be talking about Google Play services.
For those of you who don't know what it is,
it's a way for us to provide new APIs to developers
on a regular basis that will work on devices running
the latest operating system, all the way back to Gingerbread.
It contains great APIs, such as Location, Games, Wallet, G+,
and much, much more.
You can leverage all of these to create amazing apps
for your users.
To use these APIs, you can access them
through the Google Play services app, which
is delivered to users' devices through Google Play,
and updated automatically.
With the most recent release of the app,
we're moving to a new model for how developers
like you can interact more consistently
with these services.
Don't worry though.
If you've previously integrated with Google Play services,
nothing changes for your existing apps in the wild.
The binary interface of the Google Play services app
remains the same.
So previously compiled apps will continue to work.
In today's DevByte, I'll cover why
we need this change, what it is and how
to implement it, and step through an example
together with you.
So let's begin with the basics.
To use all the great APIs provided by the Google Play
services app, you need to first open up
a client connection to that process.
To do this, you include the Google Play services
SDK in your app.
And call into the SDK to set up the connection.
Now, there used to be a different client
for each service, such as the PlusClient, GamesClient,
AppStateClient, and so on.
To use three services, you would have to create three clients,
and manage independent connections
with slightly different nuances.
Fortunately, with this new and simplified interaction model,
you will only need a single GoogleApiClient
to connect to the Google Play services app.
When you set up the client, you specify
whichever APIs you want to use.
This greatly improves the developer experience
because you only have one client to worry
about connecting and handling Callbacks for.
Let's talk about how to implement it
by seeing some examples of how you would initialize
the GoogleApiClient.
This shows you how to add the Wallet API
and specify an account.
This shows the Plus API and scope.
This shows the Drive API and scope.
And now, the real difference is that now you
can add multiple APIs and scopes easily,
which is a huge win in terms of saving you development time
and complexity.
So once you have initialized a client with the APIs
you need, you have to connect and implement
Callbacks for the single GoogleApiClient, as seen here.
After you have a connected GoogleApiClient,
how do you access the methods in the API?
We can look at the case of Games as an example.
The previous model involved calling
into the GamesClient as the main entrypoint
to get things like achievements and leaderboards.
But that is now being deprecated.
So should we call it on GoogleApiClient
because that's the replacement?
Well, we can't because it's not coupled to any service.
And we don't want it to know about all the methods
that each service could potentially provide.
Instead, the answer is to use the new entrypoints
that we've created.
In the Games case, the main entrypoint
is now the Games class, which offers
static methods to read or write data.
You can access game achievements through the Games dot
Achievements class, or a leaderboard through the Games
dot Leaderboards class, and so on.
Every time you make a call to an API,
you must pass the GoogleApiClient object as well.
This serves as a reminder that you need an open connection
to the Google Play services app in order for the request
to be handled.
For more detail, you can check out the documentation or sample
games apps using the provided link.
Now, after you call an API, we've
standardized it so that you get a pending result back.
PendingResult is typed so that you can expect the answer
that you're waiting for to be of type result.
You can wait synchronously for the result as seen here,
or you can set an AsynchCallback to notify you later
when the result is ready.
Result is an interface that the services use to return you
the data you've requested, and it has a Get Status method.
The status object tells you information about
whether or not the result was successful and uses
a standard set of status codes.
Then you can proceed and use the results.
To review everything we went over,
let's walk through an example.
We are fetching the visible people in a user's G+ circles,
which can be useful, for example,
to create a social experience in an app.
First, we set up the GoogleApiClient
and set the Plus, API, Scope, and Connection Callbacks.
Then we implement the Callback methods
for Connection Succeeded, Suspended, or Failed.
The connection could fail because it
requires user intervention.
For example, to choose an account.
Then an intent would be returned in the connection result.
And you should allow the user to resolve the problem
and connect again.
Next, we call Connect and Disconnect
on the GoogleApiClient.
As soon as the connection is successful,
we can fetch the information our app
needs by calling a static method in the API
which is Plus dot People API dot load Visible.
And we pass in the GoogleApiClient,
on the PendingResult that gets returned,
we set a Callback because we want the result asynchronously.
In the Callback, we will receive a Load People
Result, which is of type result.
So we can get the status, and check
that the status code is successful.
If all is well, then we can obtain the list of people
that we're looking for from the result.
To see the full example, go to the List Visible People
Activity dot Java Class, in the samples, in the SDK.
In conclusion, we are moving towards a consistent model
of how to interact with Google Play services.
See the documentation for more details.
Existing services like Games, AppState, Wallet,
and G+ have already converted to the new model.
And more will be coming soon.
New services like Chromecast and Drive
are already working with the GoogleApiClient.
So try out Google Play services today,
and see how it can enhance your app.
Thank you for watching, and see you next time.