1 00:00:00,000 --> 00:00:04,413 [MUSIC] 2 00:00:04,413 --> 00:00:10,145 Hi, I'm Ben and in this course we're going to learn all about threads and services. 3 00:00:10,145 --> 00:00:15,225 One type of app that makes extensive use of threads and services is a music player. 4 00:00:15,225 --> 00:00:18,398 We won't be making a full-fledged music player, 5 00:00:18,398 --> 00:00:21,365 that would be way too much work for this course. 6 00:00:21,365 --> 00:00:25,465 But at the end of this course, you will have an app that plays a song. 7 00:00:25,465 --> 00:00:30,227 More than that though, you'll have an excellent understanding of threads and 8 00:00:30,227 --> 00:00:31,585 services in Android. 9 00:00:31,585 --> 00:00:33,805 I'll cover everything you need to know. 10 00:00:33,805 --> 00:00:36,405 So let's get started. 11 00:00:36,405 --> 00:00:38,805 First, we'll need to create a project. 12 00:00:38,805 --> 00:00:42,646 Let's create a new project called Music Machine. 13 00:00:45,987 --> 00:00:48,167 And let's leave everything else default. 14 00:00:50,787 --> 00:00:54,647 Except let's choose empty activity, instead of blank activity. 15 00:00:57,167 --> 00:00:58,207 And then hit Finish. 16 00:01:03,990 --> 00:01:08,375 The first thing we want our music machine to do is be able to download songs from 17 00:01:08,375 --> 00:01:09,262 the Internet. 18 00:01:09,262 --> 00:01:12,842 Let's start by adding a download button. 19 00:01:12,842 --> 00:01:18,982 Let's switch over to activity main, and delete this Hello World text view. 20 00:01:20,984 --> 00:01:25,243 Then let's add a button to the bottom of the layout, centered horizontally. 21 00:01:29,063 --> 00:01:31,065 This will be the download button. 22 00:01:31,065 --> 00:01:33,803 So let's give it an ID of download button. 23 00:01:39,823 --> 00:01:42,103 And change its text to say Download. 24 00:01:46,804 --> 00:01:51,583 Lastly, let's give our button a width of match parent, by clicking up here. 25 00:01:53,274 --> 00:01:56,381 Great, let's switch back to our activity and 26 00:01:56,381 --> 00:01:59,894 declare our button as a field at the top of our class. 27 00:02:01,814 --> 00:02:04,374 Let's call it mDownloadButton. 28 00:02:04,374 --> 00:02:08,334 Private Button 29 00:02:08,334 --> 00:02:13,515 mDownloadButton. 30 00:02:13,515 --> 00:02:17,075 Then let's initialize this button and the bottom of onCreate. 31 00:02:20,035 --> 00:02:28,516 MDownloadButton = findViewByID R.id.downloadButton. 32 00:02:31,496 --> 00:02:33,556 Alt+Enter to add the cast. 33 00:02:33,556 --> 00:02:37,237 Next up, let's add an OnClickListener to our button. 34 00:02:40,198 --> 00:02:44,397 Add a couple of lines to the bottom of onCreate, and 35 00:02:44,397 --> 00:02:50,959 we'll type mDownloadbutton.set OnClickListener, new OnClickListener. 36 00:02:53,559 --> 00:02:57,359 Now let's implement the on click method for our download button. 37 00:02:57,359 --> 00:03:02,781 Inside the OnClick method, let's start with a toast that says downloading. 38 00:03:05,099 --> 00:03:08,829 Make sure to pick the Create a new Toast option, 39 00:03:08,829 --> 00:03:14,339 then hit Enter to move to the text parameter, and write Downloading. 40 00:03:17,769 --> 00:03:22,089 Then, let's make a call to a new method called downloadSong. 41 00:03:26,449 --> 00:03:31,189 And let's use alt + enter to create this method inside our MainActivity class. 42 00:03:36,029 --> 00:03:40,949 Inside the download song method, ideally, we would download a song, 43 00:03:40,949 --> 00:03:43,655 but since downloading can be tricky and 44 00:03:43,655 --> 00:03:48,330 isn't the point of this course, we'll just pretend to download. 45 00:03:48,330 --> 00:03:53,591 So instead of downloading a song, let's just have this method wait 46 00:03:53,591 --> 00:03:59,150 ten seconds to simulate about how long it might take to download a song. 47 00:03:59,150 --> 00:04:04,367 An easy way to do this is to add ten seconds to the current time and 48 00:04:04,367 --> 00:04:08,510 then run a while loop until our ten seconds are up. 49 00:04:08,510 --> 00:04:13,184 We can get the current time by using the system from 50 00:04:13,184 --> 00:04:18,202 the Java Lang class.currenttime.Millis method. 51 00:04:18,202 --> 00:04:23,516 This method returns the difference measured in milliseconds 52 00:04:23,516 --> 00:04:29,143 between the current time and midnight January 1st, 1970. 53 00:04:29,143 --> 00:04:32,425 This might seem like a weird way to do this, but 54 00:04:32,425 --> 00:04:35,883 it actually makes working with time super easy. 55 00:04:35,883 --> 00:04:40,641 Instead of juggling hours, minutes, seconds, and whatever, 56 00:04:40,641 --> 00:04:43,503 we only need to worry about one number. 57 00:04:43,503 --> 00:04:48,664 Let's start by declaring a variable called end time. 58 00:04:48,664 --> 00:04:54,704 Which will represent a time ten seconds after when download song is called. 59 00:04:54,704 --> 00:04:59,564 But instead of using an Int, we'll need to use a long. 60 00:04:59,564 --> 00:05:02,924 There's been a lot of milliseconds since 1970. 61 00:05:02,924 --> 00:05:07,678 Let's type long endTime and 62 00:05:07,678 --> 00:05:15,010 set it equal to system.currenttimemillis 63 00:05:15,010 --> 00:05:18,386 + 10 x 1000. 64 00:05:18,386 --> 00:05:22,291 There's 1000 milliseconds in a second so 65 00:05:22,291 --> 00:05:27,907 adding 10 x 1000 is adding 10 seconds to the current time. 66 00:05:27,907 --> 00:05:32,664 We could use 10,000, instead of 10 times 1,000, but 67 00:05:32,664 --> 00:05:38,347 writing it this way makes it easier to see at a glance how much time we're adding. 68 00:05:38,347 --> 00:05:40,632 Now that we've got our end time, 69 00:05:40,632 --> 00:05:45,948 let's set up a while loop to run while the current time is before the end time. 70 00:05:47,717 --> 00:05:55,225 While system.currentTimeMillis less than endTime and 71 00:05:55,225 --> 00:05:59,297 then let's add our brackets. 72 00:06:01,177 --> 00:06:05,577 Nice, this will definitely work, but we can do better. 73 00:06:05,577 --> 00:06:10,707 Right now it's checking the current time against the end time thousands 74 00:06:10,707 --> 00:06:15,177 of times a second and that's a huge waste of processing power. 75 00:06:15,177 --> 00:06:18,950 Instead, inside the body of our while loop, 76 00:06:18,950 --> 00:06:22,337 let's wait one second before looping. 77 00:06:22,337 --> 00:06:27,658 We can do this by using the Thread.sleep method. 78 00:06:27,658 --> 00:06:32,742 And if we pass on a parameter, that's how many milliseconds 79 00:06:32,742 --> 00:06:36,638 we'll wait, or sleep, before continuing. 80 00:06:36,638 --> 00:06:42,399 Let's pass on 1,000 to wait for one second. 81 00:06:42,399 --> 00:06:49,099 Then, we can see that this sleep command might throw an interrupted exception. 82 00:06:49,099 --> 00:06:55,699 So let's use Alt+Enter to wrap this in a try catch block. 83 00:06:55,699 --> 00:07:00,431 Lastly, let's add a log message to the end of our download song 84 00:07:00,431 --> 00:07:03,120 method that says song downloaded. 85 00:07:05,240 --> 00:07:10,780 Let's type log.d TAG and all caps. 86 00:07:13,360 --> 00:07:15,140 Song downloaded. 87 00:07:18,700 --> 00:07:24,258 Alt+Enter to importLog and then let's use Alt+Enter to create our TAG constant. 88 00:07:28,418 --> 00:07:34,718 And let's set it equal to MainActivity.class.getSimpleName. 89 00:07:37,003 --> 00:07:41,069 We're all set up to start exploring the world of threads and services, 90 00:07:41,069 --> 00:07:45,490 in the next video we'll run this and see where we can make some improvements.