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 introduce bound Services and also talk about the Service lifecycle!
Song - If it starts playing right away, you can download it by right clicking (CTRL + click for Mac) on the link and choosing "Save link as...".
Related Links
[MUSIC]
0:00
So far, we've only seen one
type of service started, but
0:04
remember there's two ways
we can use a service.
0:09
In addition to starting a service
we can also bind to a service.
0:13
Binding to a service is for
0:17
when we need to interact with our service
in more ways than just starting it.
0:19
If we want to communicate with
our service after starting it,
0:24
we'll need to buy into it.
0:27
If you're familiar with
the client server model,
0:30
abound to service can be
thought of as the server.
0:32
And the activity binding to that service
can be thought of as the client.
0:36
A good example would be a service used for
music playback.
0:40
Once the service is playing music,
0:44
there's all kinds of things
we could want to tell it.
0:47
Like, play a song or
skip to the next song.
0:49
Let's get started with bound services
by adding the ability to play songs
0:54
to our music machine app.
0:58
First, let's create a new class for
1:00
music playing service and
let's name it Player Service.
1:03
Then, let's make it extend
to the service class and
1:12
use alt enter to implement
the required on-bind method.
1:18
All right, before we go on, let's
head over to our Android manifest and
1:24
declare our new service,
use command or control D to duplicate
1:30
and change download intent service
to player service, and we're done.
1:37
Let's also take this opportunity to get
rid of some of the tabs we won't be
1:44
needing for a while.
1:48
Let's close all of our classes that start
with download as well as the Android
1:50
manifest.
1:54
Now that we've got our service, let's take
a minute to go over what we'll be doing.
2:00
The goal is for us to be able to play and
pause a single song using a bound service.
2:05
Doing this with a service is what will
allow us to have music playback in
2:12
the background,
even when our activity is closed.
2:15
However, music playback is
not the goal of this course.
2:20
So I won't be going into
too much detail about it.
2:24
Sounds like we need a song.
2:28
That's why I put one in
the teacher's notes below.
2:30
Go ahead and download it.
2:33
Then let's create a new resource directory
named Raw where we can store our MP3 file.
2:35
Right click on the res directory,
New, Android Resource Directory,
2:43
pick Raw as the resource type and
then hit OK.
2:50
Then let's copy and
paste the song into our new raw directory.
2:59
Copy.
3:06
Paste.
3:09
And now we'll be able to
use this song in our code.
3:12
Cool.
3:15
Back in our player service class
we'll be using a media player object
3:17
to handle playing and pausing our song.
3:21
Let's create a new field for our media
3:25
player by typing private media player,
M player.
3:30
Now to initialize it.
3:37
We only need one media player,
so where should we create it.
3:41
Well, to answer that question,
3:45
we need to have a better understanding
of the service lifecycle.
3:48
Luckily, the lifecycle of a service is
much simpler than that of an activity.
3:52
For a Started service [SOUND] we start
with the call to startService [SOUND].
3:57
Then if our service hasn't been created
yet, its onCreate [SOUND] method is
4:01
called followed by a call
[SOUND] to onStartCommand.
4:05
And when a Started service is stopped,
it's onDestroy method will be called.
4:09
For a bound service we start with a call
to bindService, then if our service hasn't
4:14
been created yet it's onCreate method is
called, followed by a call to onBind.
4:20
Once all the clients from our
bound service have unbound
4:26
the onUnbind method will be called
followed by one final call to onDestroy.
4:30
We can also have a service that is both
started and bound, which means onStart
4:36
Command was called, and there's at least
one client still bound to the service.
4:41
In this case on destroy won't be called
until the service has been stopped and
4:47
all of the clients have on bound from
the service, that wasn't so bad, right?
4:53
Back to the question, where should
we initialize our media player?
4:58
Well, we only need one media player.
5:02
So, we probably shouldn't
initialize end on bind.
5:06
Since on bind is called each time
a client binds to our service.
5:09
Rather let's initialize it and
5:14
on create which is only called once
during the life cycle of a service.
5:16
Let's add some space above on bind, and
5:21
use control O to open
the over ride dialog.
5:24
Then let's override
the on create method and
5:28
get on with an initializing
our media player.
5:32
Let's get rid of this
call to super.oncreate,
5:36
calling super isn't required for
the service lifecycle.
5:39
Then let's type mPlayer
= MediaPlayer.create,
5:43
pass in our context by using the this
keyword, and pass in our resource ID for
5:51
the song, R.raw.jingle, voila!
5:57
In the next video will see how we can
use this media player to play and
6:05
pause our song.
6:09
You need to sign up for Treehouse in order to download course files.
Sign up