1 00:00:00,940 --> 00:00:05,500 Using a service to handle intense one at a time on a separate thread 2 00:00:05,500 --> 00:00:07,640 like we've done with our download service, 3 00:00:07,640 --> 00:00:12,130 is one of the most common ways that developers make use of services. 4 00:00:12,130 --> 00:00:16,800 In fact, it's so common that Android created a special class 5 00:00:16,800 --> 00:00:19,510 to make writing a service like this, a lot easier. 6 00:00:20,600 --> 00:00:23,270 And that class is intent service. 7 00:00:23,270 --> 00:00:26,470 Let's see how we can refactor our code to use an intent service. 8 00:00:28,150 --> 00:00:33,929 Let's start by creating a new class for our intent service and 9 00:00:33,929 --> 00:00:37,830 let's name it DownloadIntentService. 10 00:00:40,220 --> 00:00:43,709 Then let's make it extend IntentService, 11 00:00:43,709 --> 00:00:48,625 and let's use Alt+Enter to implement the required method. 12 00:00:51,388 --> 00:00:55,340 This onHandleIntentMethod is where we'll handle our intent. 13 00:00:56,640 --> 00:01:01,810 Now before we forget it, Let's head over to our AndroidManifest and 14 00:01:01,810 --> 00:01:03,700 declare our new service. 15 00:01:03,700 --> 00:01:06,960 After you create an application component like a service or 16 00:01:06,960 --> 00:01:11,740 activity, it's usually a good idea to add it to your manifest right away. 17 00:01:12,760 --> 00:01:14,620 It's super easy to forget about and 18 00:01:14,620 --> 00:01:19,270 it will definitely throw an error, trust me I've tested it. 19 00:01:19,270 --> 00:01:22,830 We'll need to declare it just like we did with download service, 20 00:01:22,830 --> 00:01:27,320 which also gives me an opportunity to show off one of my favorite keyboard shortcuts. 21 00:01:28,460 --> 00:01:32,260 Place your cursor anywhere on the download service line and 22 00:01:32,260 --> 00:01:36,240 hit command or control D, which stands for duplicate. 23 00:01:37,340 --> 00:01:43,260 Now we just have to change download service to download intense service and 24 00:01:43,260 --> 00:01:43,760 we're done. 25 00:01:45,660 --> 00:01:48,300 But it looks like we need to add a constructor 26 00:01:48,300 --> 00:01:49,650 to our download intense service. 27 00:01:51,140 --> 00:01:56,626 Back in our intense service, let's add that constructor, public 28 00:01:56,626 --> 00:02:05,570 DownloadIntentService. 29 00:02:05,570 --> 00:02:08,760 And inside the constructor, lets add a call to super 30 00:02:12,450 --> 00:02:15,868 and pass in DownloadIntentService as a string. 31 00:02:15,868 --> 00:02:20,128 This sets 32 00:02:20,128 --> 00:02:26,030 the name of the thread where our intense service will be doing its work. 33 00:02:26,030 --> 00:02:30,254 Then, let's add a call to 34 00:02:30,254 --> 00:02:36,800 setIntentRedelivery and pass in true. 35 00:02:36,800 --> 00:02:41,410 By default, an intent service will use start sticky. 36 00:02:41,410 --> 00:02:45,180 But we needed to use start redeliver intent. 37 00:02:45,180 --> 00:02:49,640 This method lets us change on start commands return value for 38 00:02:49,640 --> 00:02:53,210 our intent service, even though we can't see it. 39 00:02:53,210 --> 00:02:57,747 All right, now let's copy our download song method from download handler. 40 00:03:02,498 --> 00:03:07,501 And paste it in the bottom of our IntentService class and 41 00:03:07,501 --> 00:03:09,853 create our TAG constant. 42 00:03:20,398 --> 00:03:23,000 Get SimpleName. 43 00:03:23,000 --> 00:03:24,920 And I'll get rid of this extra space up here. 44 00:03:26,050 --> 00:03:29,120 Then and the On handle intent method. 45 00:03:29,120 --> 00:03:33,818 Let's retrieve our song from the intent and pass it to the download song method. 46 00:03:33,818 --> 00:03:41,873 String_song = intent.getStringExtra 47 00:03:41,873 --> 00:03:48,138 MainActivity.KEY_SONG; and 48 00:03:48,138 --> 00:03:53,070 then downloadSong song. 49 00:03:53,070 --> 00:03:58,469 Finally, over in MainActivity Inside 50 00:03:58,469 --> 00:04:02,548 our download buttons OnClickListener lets start our 51 00:04:02,548 --> 00:04:07,089 DownloadIntentService instead of our download service. 52 00:04:11,825 --> 00:04:13,290 Perfect! 53 00:04:13,290 --> 00:04:17,255 Now let's test the app and make sure it still works just like it did before. 54 00:04:24,478 --> 00:04:29,023 And if we tap the download button, 55 00:04:29,023 --> 00:04:33,568 and change our log level to debug, 56 00:04:33,568 --> 00:04:38,764 we can see our song start downloading. 57 00:04:38,764 --> 00:04:44,311 Then in the Android Device Monitor, we can kill the process. 58 00:04:49,276 --> 00:04:56,750 And we can see that our service is scheduled to be restarted in 52 seconds. 59 00:04:58,010 --> 00:04:58,798 At least for me. 60 00:05:01,478 --> 00:05:04,860 And once it's restarted our songs will continue downloading. 61 00:05:06,120 --> 00:05:09,780 Using an intense service like this is exactly the same 62 00:05:09,780 --> 00:05:11,400 as what we were doing before. 63 00:05:12,510 --> 00:05:17,620 In fact you can delete download service, download thread and 64 00:05:17,620 --> 00:05:20,590 download handler because we're not using them anymore. 65 00:05:21,640 --> 00:05:24,900 But you might want to keep them around as a reference to look back to later. 66 00:05:26,170 --> 00:05:30,140 We've learned so much about services in such a short time. 67 00:05:30,140 --> 00:05:32,030 I know this stuff can seem difficult but 68 00:05:32,030 --> 00:05:35,480 the more time you spend with it, the easier it gets. 69 00:05:35,480 --> 00:05:39,750 Remember, if you have any questions, don't hesitate to ask in the community.