This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Google Play Services!
You have completed Google Play Services!
Preview
Now that you have got some content to interact with, connect with Google Play Services.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Now that we've got some
content to interact with,
0:04
let's connect with Google Play Services.
0:07
The way Google Play Services works is you
connect to the service when your activity
0:10
starts up and you disconnect when
you're leaving your activity.
0:14
There are various states that connection
to Google Place Services can be in, and
0:18
we'll need to handle all of those.
0:22
Let's talk about how this works.
0:24
You connect to Google Place Services
using a GoogleApiClient object.
0:26
To handle the various states, you supply
it with two different interfaces.
0:31
ConnectionCallbacks and
OnConnectionFailedListener.
0:35
Each of these adds in methods you
must provide implementations for.
0:39
You start using Google Play Services by
calling connect on your GoogleApiClient.
0:43
If your successful,
you get a callback on onConnected.
0:48
If a disconnect happens after
your initial connection
0:52
you get a callback to
onConnectionSuspended().
0:55
If the initial connection is unsuccessful
0:59
you get a callback to
onConnectionFailed().
1:01
Reasons for failure include the version
of Google Play Services is out of date or
1:04
needs an update through Google Play Store.
1:09
Or they're disabled all
together on the user's device.
1:11
To determine if we can
proceed we can callback
1:14
to Google Play Services
to resolve any errors.
1:17
The response to this comes back through
onActivityResult() where we can either try
1:20
to connect again or simply fail due to
a fatal error we cannot recover from.
1:24
As you can see,
1:30
handling all these states really starts
to bloat our code in our activity.
1:31
The activity really needs to only
know two things to display a UI.
1:36
If it's connected or disconnected.
1:40
To simplify the interaction with our
activity, let's create a helper class.
1:42
The new model looks like this.
1:46
We move all the logic from dealing
with Google Play Services state
1:48
into a GoogleServicesHelper class.
1:51
This class will implement all the methods
required by the GoogleApiClient.
1:54
Then we create an interface with
two simple methods, onConnected or
1:59
onDisconnected.
2:03
Whatever is displayed in the UI can
implement just these two methods to know
2:04
if they're connected or not.
2:08
Let's create our helper class now.
2:10
I'm gonna create a new package and
this package is gonna be called google.
2:11
And in here, we're create our
Google helper object class and
2:17
I call it GoogleServicesHelper.
2:22
Okay, the first thing we need
to do is create an interface to
2:24
model the interaction with UI, so
I'm just gonna include that right here.
2:29
In order to find the interface, and
2:34
I'm gonna call it GoogleServicesListener.
2:38
And it has two methods.
2:42
OnConnected, onDisconnected.
2:44
Again these are the only things that
our UI is going to need to know.
2:50
The GoogleServicesHelper is gonna
handle all those edge cases and
2:53
error checking and other scenarios
we need to encounter and check for.
2:56
Next, let's create our constructor.
3:01
The constructor needs two things.
3:03
The first is an activity.
3:06
The second is gonna be the listener.
3:13
The listener we're gonna go ahead and
keep a reference to.
3:20
The activity we need to initialize are
GoogleApiClient object because as usual
3:29
it needs a context.
3:34
First let's store this
activity in our class.
3:36
Next we need to store
a GoogleApiClient object.
3:46
We can create an apiClient by using the
convenient GoogleApiClient.Builder class,
3:54
it requires us to pass in the context.
4:01
We're gonna use the activity.
4:04
And now we can go ahead and
set the connection callbacks and
4:06
the onConnectionFailed listener.
4:09
Remember, our services helper object is
the, going to be the one that actually
4:13
implements this interface, so
we're just gonna pass in this.
4:18
Finally we just call build.
4:24
We need to go ahead and
implement these two interfaces.
4:27
And we're gonna go ahead and implement the
methods that are required by each of them.
4:36
OnConnected, onConnectionSuspended,
and onConnectionFailed.
4:41
I'm gonna go ahead and
move these to the body of our class.
4:45
Okay, and now that we've got all the basic
parts in place we can take a break.
4:52
Next step, we need to handle
the various states in our helper class.
4:55
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up