Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that you have the basic architecture in place you need to fill out our Helper class to make it functional.
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
Now that we have the basic architecture in
place,
0:01
we need to fill out our helper class to
make it functional.
0:03
According to the documentation, you should
initiate talking to Google Play services
0:06
by calling connect in on start, and
disconnect in on stop of your activity.
0:10
We need a way to do this with our helper,
so let's add in those methods now.
0:16
We're just gonna go ahead and add in a few
public methods.
0:20
This is what our activity's gonna call
through too.
0:25
It's gonna call connect.
0:27
And we're simply going to pass this
through to our API client.
0:32
And with disconnect we're gonna do the
same thing.
0:38
And one thing to think about when we're
writing code like this is that
0:41
what happens if Google Play services isn't
available, what should we do then?
0:46
Well, we should check that and handle
those cases to make sure our code
0:50
doesn't crash, or throw a null pointer
exception, or another error.
0:55
So let's go ahead and add in a method, and
what this is gonna do is just
0:58
check if Google Play services is possibly
available for us to connect to.
1:03
It doesn't guarantee we'll be able to
connect, but
1:08
it's just saying is this available on the
device at all?
1:10
So let's go ahead and check this.
1:12
It's gonna be a boolean.
1:14
So it'll return back if it's connected or
if it's available or
1:15
if it's not available.
1:18
And we're just gonna call it is Play, is
Google Play services, or
1:20
PlayServicesAvailable.
1:25
And the great thing is that Google Play
Services has
1:30
a utility class built in to help us verify
this.
1:33
And the call we're gonna make is
GooglePlayServicesUtil.
1:37
So in here, and we're gonna call
isPlayServicesAvailable.
1:43
And it needs a context, so we're gonna go
ahead and use that activity we stored.
1:48
And this is gonna return back to us an
integer about the availability.
1:53
Now what we're gonna do is go ahead and
1:57
switch on that integer and Google Play
services has
2:02
also included some constants to know what
the result of that availability is.
2:08
So, for one, there's a case stored on the
ConnectionResult class and
2:14
that is SUCCESS.
2:19
So, in this case, yeah, Google Play
services is available,
2:22
you wanna return true.
2:24
The other cases are cases where a, ab
disabled, or
2:27
an update is needed, something like that.
2:32
So, we're gonna handle those cases as
well, but we're going to put them all
2:34
under the same case because they're all
gonna have the same result.
2:38
We need to figure out a way to resolve
that error.
2:42
So, let's go ahead and add in those cases
here.
2:44
So there's SERVICE_INVALID, DISABLED,
UPDATE_REQUIRED.
2:49
So these three right here, we can all do
the same thing with these.
2:54
So let's go ahead and add in all these
cases together.
2:57
[BLANK_AUDIO]
3:01
SERVICE_INVALID.
3:09
Okay and what we're gonna do here is now,
3:11
the util has provided an additional method
for us.
3:13
And what this is gonna do is, it's gonna
display a dialogue to the user.
3:17
And hopefully, the user will be able to
resolve this error, and
3:21
we'll be able to connect eventually.
3:26
So, let's go ahead and use
GooglePlayServicesUtil, and
3:28
there's something called getErrorDialogue.
3:32
And what we need to do here,
3:37
is pass in the availability code we got
back from the other util method.
3:39
And you pass in our activity.
3:44
And then we need to pass in a request
code.
3:46
So what this is doing underneath is this
is calling though to start activity for
3:48
results.
3:53
And it's displaying this dialogue to the
user.
3:54
So it's gonna display another activity to
the user.
3:56
And we need to know if that request code
comes back, what the result of that was.
3:59
So we need a unique request code here.
4:04
So let's go ahead and define a unique
request code in the top of our class.
4:06
And we're gonna define one right here.
4:11
Private static final int REQUEST_CODE.
4:13
And I'm gonna call this one AVAILABILITY.
4:18
And it's gonna give it a unique value
here, -101.
4:21
And I have to find another one up here,
-100.
4:26
We're gonna use that in a minute.
4:29
And again, these are because we're calling
through to start activity for result.
4:31
We need a unique code for every time we
call through to that.
4:35
In this case, we're calling it multiple
times.
4:39
So that means we need to know when it
comes back, which one am I handling?
4:41
So that's why these values are unique.
4:45
We're going to go ahead and do
REQUEST_CODE_AVAILABILITY.
4:48
And we have to call show on this, cuz this
actually returns back a dialog object.
4:51
So we need to call show otherwise it won't
be shown to the user.
4:58
In this case we're gonna return false
because we aren't sure that Google Play
5:00
services are available but using this
error dialogue we'll go ahead and
5:06
display something to the user and we might
come back and connect after all but for
5:12
now we just don't know, and by default
we're just gonna return false.
5:16
Now that we've handled those various
states, lets take a break.
5:21
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