This course will be retired on June 1, 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
Preview
Start a free Courses trial
to watch this video
In this video we’ll learn what it means to stop a started Service!
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
We just killed our process and
watched it rise from the grave.
0:00
But why?
0:04
Well, this is one of the big differences
between a service and an activity.
0:06
When you kill an app's process,
everything in that app is killed,
0:11
including all the activities and services.
0:15
But depending on what we return from
onStartCommand the services might
0:19
come right back.
0:23
Let's take another look at what we're
returning from onStartCommand look
0:26
down here we're returning
a constant from the service class
0:30
called START_REDELIVER_INTENT.
0:35
This is one of the three values that we
can return from onStartCommand to tell
0:37
Android how to handle our service being
killed, while it's still working.
0:42
But how do we determine if our
service is still working or not?
0:47
Easy.
0:52
A service is considered started if
onStartCommand has returned a value.
0:53
And it's not considered stopped
until it's explicitly stopped.
1:00
So the return value from onStartCommand
represents what we want Android to do
1:05
if our service's process is killed after
we've returned from onStartCommand,
1:11
but before being stopped.
1:17
Let's take a quick tour to talk
more about stopping a service.
1:21
Stopping a service, let's Android
know that we're finished with it and
1:25
frees up more room for other applications.
1:29
We can stop a service by calling
stopSelf from the service itself or
1:32
if we need to stop our service from
the outside We can call stopService and
1:38
pass an intent,
just like when we started our service.
1:44
Now let's see how we can stop our
download service when it's done working.
1:48
The purpose of download service
is to download one song.
1:54
Remember that we call start service for
each song in the playlist.
1:59
So when that one song is
finished downloading,
2:04
our service is done working and it would
be a good time to stop the service.
2:08
Over and download handler and
side the handle message
2:13
method is the call to download song.
2:18
Once this call is finished,
our song has been downloaded.
2:25
Let's add a line after this call and
stop our service.
2:30
At first,
since we don't have access to our service,
2:35
it seems like we should do this by calling
stop service and passing an intent.
2:39
but this isn't a good approach for us.
2:45
Calling stop service,
stops the service entirely.
2:48
If we called stop service
after only the first song,
2:53
we'd be stopping the service
before it finished working.
2:57
Not good.
3:00
Instead, there's a version of the stop
self method that takes an integer When
3:02
onStartCommand is called, one of the
parameters is an integer named startID.
3:07
Calling stops self and passing in
this startID makes sure that we don't
3:16
stop our service until it has
handled all of the startIDs.
3:21
Okay, so we need to get the startID from
onStartCommand and pass it to our handler.
3:26
Then, when downloading is finished,
3:33
we need to use the correctonStartCommand
and call stopSelf on our service.
3:36
Back in download service,
right above where we send our message,
3:42
let's attach the startID to our message
using one of the argument properties.
3:47
Each message object has two
integer properties named arg1 and
3:53
arg2, which we can use for
passing arguments.
3:59
Let's set arg1 Equal to startId.
4:04
Now, we just need to call stop self on our
service when our handler's done working.
4:10
An easy way to do this would be to just
pass a reference to our service to
4:17
our handler.
4:22
And the bottom of the OnCreate Let's type
4:23
mhandler.setService.
4:29
And pass in the keyword this
to pass in our service.
4:35
Then let's use Alt Enter
to create a setter.
4:40
Hit Enter twice to accept the defaults and
there we go.
4:47
Let's flip back to the download
handler class and see what we've got.
4:52
Looks like we've got a field for
our service named mService.
4:58
And we've got a new method called,
setService as well.
5:04
The last thing we need to do,
5:09
is use mService to call stop self
at the bottom of handle message.
5:10
Let's type mService.stopSelf and
5:15
pass in msg.arg1 to make sure that we only
5:20
stop the service if it's
handled all of our startIDs.
5:25
Great work.
5:33
Let's quickly test this out
to see what's different.
5:34
Make sure we also have open
the Android device monitor.
5:42
And hit the Download button.
5:49
Give it ten seconds to download a song.
5:53
And then let's kill the process.
6:02
and there's our message saying,
it'll restart and
6:09
if we wait we should see
our service restart and
6:14
finish downloading our
songs just like last time.
6:20
I'll just skip to the end, but
6:23
pause me until the downloads
are finished if you're following along.
6:25
Okay.
6:30
So what happens if we kill the process
once all the songs have downloaded?
6:31
Let's do it.
6:36
And if we look to the lawn, it doesn't
look like our service was rescheduled.
6:42
Perfect.
6:49
That's why it's so important to make
sure that we stop our services.
6:51
If we don't, they can hog system
existing resources almost forever.
6:55
Services are an incredibly powerful tool
and any Android developers tool belt But
7:01
with great power comes
great responsibility, and
7:06
if we forget to stop our started services,
then we won't have very happy users.
7:09
In the next video, we'll get back to
where we left off with onStartCommand.
7:14
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