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 talk about what we’re trying to accomplish, and then we’ll create our project!
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
[MUSIC]
0:00
Hi, I'm Ben and in this course we're going
to learn all about threads and services.
0:04
One type of app that makes extensive use
of threads and services is a music player.
0:10
We won't be making
a full-fledged music player,
0:15
that would be way too much work for
this course.
0:18
But at the end of this course,
you will have an app that plays a song.
0:21
More than that though, you'll have
an excellent understanding of threads and
0:25
services in Android.
0:30
I'll cover everything you need to know.
0:31
So let's get started.
0:33
First, we'll need to create a project.
0:36
Let's create a new project
called Music Machine.
0:38
And let's leave everything else default.
0:45
Except let's choose empty activity,
instead of blank activity.
0:50
And then hit Finish.
0:57
The first thing we want our music machine
to do is be able to download songs from
1:03
the Internet.
1:08
Let's start by adding a download button.
1:09
Let's switch over to activity main,
and delete this Hello World text view.
1:12
Then let's add a button to the bottom
of the layout, centered horizontally.
1:20
This will be the download button.
1:29
So let's give it an ID of download button.
1:31
And change its text to say Download.
1:39
Lastly, let's give our button a width
of match parent, by clicking up here.
1:46
Great, let's switch back
to our activity and
1:53
declare our button as a field
at the top of our class.
1:56
Let's call it mDownloadButton.
2:01
Private Button
2:04
mDownloadButton.
2:08
Then let's initialize this button and
the bottom of onCreate.
2:13
MDownloadButton = findViewByID
R.id.downloadButton.
2:20
Alt+Enter to add the cast.
2:31
Next up, let's add
an OnClickListener to our button.
2:33
Add a couple of lines to
the bottom of onCreate, and
2:40
we'll type mDownloadbutton.set
OnClickListener, new OnClickListener.
2:44
Now let's implement the on click
method for our download button.
2:53
Inside the OnClick method, let's start
with a toast that says downloading.
2:57
Make sure to pick the Create
a new Toast option,
3:05
then hit Enter to move to the text
parameter, and write Downloading.
3:08
Then, let's make a call to a new
method called downloadSong.
3:17
And let's use alt + enter to create this
method inside our MainActivity class.
3:26
Inside the download song method,
ideally, we would download a song,
3:36
but since downloading can be tricky and
3:40
isn't the point of this course,
we'll just pretend to download.
3:43
So instead of downloading a song,
let's just have this method wait
3:48
ten seconds to simulate about how long
it might take to download a song.
3:53
An easy way to do this is to add
ten seconds to the current time and
3:59
then run a while loop until
our ten seconds are up.
4:04
We can get the current time
by using the system from
4:08
the Java Lang
class.currenttime.Millis method.
4:13
This method returns the difference
measured in milliseconds
4:18
between the current time and
midnight January 1st, 1970.
4:23
This might seem like
a weird way to do this, but
4:29
it actually makes working
with time super easy.
4:32
Instead of juggling hours,
minutes, seconds, and whatever,
4:35
we only need to worry about one number.
4:40
Let's start by declaring
a variable called end time.
4:43
Which will represent a time ten seconds
after when download song is called.
4:48
But instead of using an Int,
we'll need to use a long.
4:54
There's been a lot of
milliseconds since 1970.
4:59
Let's type long endTime and
5:02
set it equal to system.currenttimemillis
5:07
+ 10 x 1000.
5:15
There's 1000 milliseconds in a second so
5:18
adding 10 x 1000 is adding 10
seconds to the current time.
5:22
We could use 10,000,
instead of 10 times 1,000, but
5:27
writing it this way makes it easier to see
at a glance how much time we're adding.
5:32
Now that we've got our end time,
5:38
let's set up a while loop to run while
the current time is before the end time.
5:40
While system.currentTimeMillis
less than endTime and
5:47
then let's add our brackets.
5:55
Nice, this will definitely work,
but we can do better.
6:01
Right now it's checking the current
time against the end time thousands
6:05
of times a second and
that's a huge waste of processing power.
6:10
Instead, inside the body
of our while loop,
6:15
let's wait one second before looping.
6:18
We can do this by using
the Thread.sleep method.
6:22
And if we pass on a parameter,
that's how many milliseconds
6:27
we'll wait, or sleep, before continuing.
6:32
Let's pass on 1,000 to wait for
one second.
6:36
Then, we can see that this sleep command
might throw an interrupted exception.
6:42
So let's use Alt+Enter to wrap
this in a try catch block.
6:49
Lastly, let's add a log message
to the end of our download song
6:55
method that says song downloaded.
7:00
Let's type log.d TAG and all caps.
7:05
Song downloaded.
7:13
Alt+Enter to importLog and then let's use
Alt+Enter to create our TAG constant.
7:18
And let's set it equal to
MainActivity.class.getSimpleName.
7:28
We're all set up to start exploring
the world of threads and services,
7:37
in the next video we'll run this and
see where we can make some improvements.
7:41
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