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