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
In this video we setup our app to start the binding process. We’ll see where we bind, where we unbind, and we’ll learn about ServiceConnections!
Related Links
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
If we want a working music player,
we'll need a way to call the music
0:00
playback methods in our service,
when we tap buttons in our activity.
0:04
So how can we pass our
service to our activity?
0:09
Let's start in main activity and
talk about what we'll need to be coding.
0:13
First, we should decide where in our
activity to buy into the service and
0:18
where to unbind.
0:23
Users are only able to interact with
our app when it's in the foreground.
0:25
So we could probably get away
with binding an onResume and
0:30
unbinding an onPause, but that could be
a lot of binding just for a little pop up.
0:34
Instead, we'll do the binding and
onStart and unbinding in onStop.
0:41
This way if our app is visible,
we'll be bound to our service and
0:46
able to use our music player.
0:51
We could also bind an onCreate an unbind
and onDestroy either one works.
0:53
All right let's get started.
1:00
At the bottom of main activity,
let's add some space and
1:02
override the onStart method.
1:06
Control O onStart and
make sure to pick the right one.
1:08
While we're at it, let's also override
the onStop method below onStart.
1:17
When we bind it to a service,
we use the bind a service method,
1:27
and just like the start service method,
it takes an intent.
1:31
Back onStart, below the call to super,
let's create an intent for
1:35
our player service class, intent, intent
1:41
equals new intent pass in the context
1:46
using the this keyword and
pass end play service dot class.
1:51
On the next line, let's bind to
our service by typing bindService.
1:59
And note that we need three arguments.
2:07
The intent for our service, something
called a ServiceConnection, and any flags.
2:10
Let's pass and our intent for the first
parameter skip the ServiceConnection for
2:17
now and
pass in context.BIND_AUTO_CREATE for
2:23
the third parameter, to have our service
automatically created when we bind to it.
2:30
Okay, let's get back to
that service connection.
2:37
Binding and
unbinding don't happen immediately.
2:41
The service connection is
what we used to tell us,
2:45
when we're successfully connected and
also when we've been disconnected.
2:47
When we call bindService, Android starts
building a connection between our activity
2:53
and our service and when that
connection is finally established,
2:59
Android will call the onService connected
method of our service connection that we
3:04
provide as a parameter And if we ever
get disconnected from our service
3:08
Android will let us know why calling
the onServiceDisconnected method.
3:14
In addition to binding, we'll also need
to use our service connection when we
3:20
unbind and the onStop method.
3:24
So if we need our service connection and
onStart and
3:27
onStop, we'll need to
create it as a field.
3:31
At the top of our class below our buttons,
let's add a field for
3:35
our service connection,
private ServiceConnection,
3:41
mService connection.
3:46
We still need to override
the onService connected and
3:50
the onService disconnected methods
of our service connection.
3:53
We could create our own class that
implements service connection,
3:57
override those two methods, and
then create a new instance of that class
4:01
Or we could just use an anonymous class.
4:08
If you've never used anonymous
classes before, you're lying.
4:17
There's one right here.
4:21
An anonymous class is pretty much
just a way to extend a class
4:24
without having to create
a whole new class.
4:28
It's a way for
us to add functionality to an object
4:31
without needing to create
a new type of object.
4:34
Let's set our ServiceConnection to be = to
a new anonymous ServiceConnection class.
4:38
So = new ServiceConnection.
4:45
Hit enter and there we go.
4:50
Back in the on start method let's pass
on our new ServiceConnection field as
4:53
the second parameter to bind service.
4:58
And an onStop below the call to super,
let's call the unbindService method.
5:06
And, as promised,
it takes a service connection parameter.
5:15
Let's pass that in.
5:19
We're almost done.
5:23
But unfortunately, unbindService can throw
an error if the service isn't bound.
5:25
One way this can happen is if
our call to bind the service
5:31
wasn't successful in the first place.
5:34
If we didn't bind to our service,
how can we expect to unbind from it?
5:37
We'll fix this by adding a boolean
field named mBound to track whether or
5:42
not we are bound to the service.
5:47
Below our key declaration, Let's
5:49
add this new field private boolean mBound.
5:55
And let's set it = false.
6:01
Now, we need to update our inbound
variable when our service is bound and
6:06
when it's unbound.
6:10
But how can we tell when
our service is bound.
6:12
That's right!
6:16
The onServiceConnected method
of our service connection.
6:17
And when our services unbound or
6:21
disconnected, the onServiceDisconnected
method will be called.
6:23
So when onServiceConnected,
let set mBound = to true and
6:28
then onServiceDisconnected Let's
set mBound equal to false.
6:35
Last but not least,
let's make sure we're only calling
6:46
unbind service if our
service is actually bound, so
6:51
if mBound And
add the other bracket at the bottom.
6:56
Then right below onbindService,
let's set mBound to false.
7:03
On service disconnected is only called
when something unexpected happens.
7:13
So calling unbind a service won't result
in a call to on service disconnected,
7:19
which is why we're responsible for
7:24
updating mBound after
calling unbind service.
7:26
We're almost done binding to our service.
7:31
Can you hear the music yet,
me neither, but
7:33
I think we might hear
some in this next video.
7:38
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