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 about IntentServices and refactor our app to use one!
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
Using a service to handle intense
one at a time on a separate thread
0:00
like we've done with our download service,
0:05
is one of the most common ways that
developers make use of services.
0:07
In fact, it's so common that
Android created a special class
0:12
to make writing a service like this,
a lot easier.
0:16
And that class is intent service.
0:20
Let's see how we can refactor our
code to use an intent service.
0:23
Let's start by creating a new class for
our intent service and
0:28
let's name it DownloadIntentService.
0:33
Then let's make it extend IntentService,
0:40
and let's use Alt+Enter to
implement the required method.
0:43
This onHandleIntentMethod is
where we'll handle our intent.
0:51
Now before we forget it,
Let's head over to our AndroidManifest and
0:56
declare our new service.
1:01
After you create an application
component like a service or
1:03
activity, it's usually a good idea to
add it to your manifest right away.
1:06
It's super easy to forget about and
1:12
it will definitely throw an error,
trust me I've tested it.
1:14
We'll need to declare it just like
we did with download service,
1:19
which also gives me an opportunity to show
off one of my favorite keyboard shortcuts.
1:22
Place your cursor anywhere on
the download service line and
1:28
hit command or control D,
which stands for duplicate.
1:32
Now we just have to change download
service to download intense service and
1:37
we're done.
1:43
But it looks like we
need to add a constructor
1:45
to our download intense service.
1:48
Back in our intense service,
let's add that constructor, public
1:51
DownloadIntentService.
1:56
And inside the constructor,
lets add a call to super
2:05
and pass in DownloadIntentService
as a string.
2:12
This sets
2:15
the name of the thread where our
intense service will be doing its work.
2:20
Then, let's add a call to
2:26
setIntentRedelivery and pass in true.
2:30
By default,
an intent service will use start sticky.
2:36
But we needed to use
start redeliver intent.
2:41
This method lets us change on
start commands return value for
2:45
our intent service,
even though we can't see it.
2:49
All right, now let's copy our download
song method from download handler.
2:53
And paste it in the bottom of
our IntentService class and
3:02
create our TAG constant.
3:07
Get SimpleName.
3:20
And I'll get rid of this
extra space up here.
3:23
Then and the On handle intent method.
3:26
Let's retrieve our song from the intent
and pass it to the download song method.
3:29
String_song = intent.getStringExtra
3:33
MainActivity.KEY_SONG; and
3:41
then downloadSong song.
3:48
Finally, over in MainActivity Inside
3:53
our download buttons
OnClickListener lets start our
3:58
DownloadIntentService instead
of our download service.
4:02
Perfect!
4:11
Now let's test the app and make sure
it still works just like it did before.
4:13
And if we tap the download button,
4:24
and change our log level to debug,
4:29
we can see our song start downloading.
4:33
Then in the Android Device Monitor,
we can kill the process.
4:38
And we can see that our service is
scheduled to be restarted in 52 seconds.
4:49
At least for me.
4:58
And once it's restarted our
songs will continue downloading.
5:01
Using an intense service like
this is exactly the same
5:06
as what we were doing before.
5:09
In fact you can delete download service,
download thread and
5:12
download handler because
we're not using them anymore.
5:17
But you might want to keep them around
as a reference to look back to later.
5:21
We've learned so
much about services in such a short time.
5:26
I know this stuff can seem difficult but
5:30
the more time you spend with it,
the easier it gets.
5:32
Remember, if you have any questions,
don't hesitate to ask in the community.
5:35
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