1 00:00:00,081 --> 00:00:05,410 When you play music on your phone, it's typically accompanied by a notification. 2 00:00:05,410 --> 00:00:09,032 This notification usually tells you what song is playing and 3 00:00:09,032 --> 00:00:12,594 provides some basic functionality, like play and pause. 4 00:00:12,594 --> 00:00:16,150 But this notification also serves another purpose. 5 00:00:16,150 --> 00:00:19,120 If you want your service to run in the foreground, 6 00:00:19,120 --> 00:00:21,395 then you need to have a notification. 7 00:00:21,395 --> 00:00:25,869 A foreground service is a service that the user is actively aware of. 8 00:00:25,869 --> 00:00:29,081 Thus a foreground service is given special priority 9 00:00:29,081 --> 00:00:32,670 when Android encounters low memory situations. 10 00:00:32,670 --> 00:00:36,220 Remember the various levels of importance for processes? 11 00:00:36,220 --> 00:00:39,730 Well, when we designate our service as a foreground service, 12 00:00:39,730 --> 00:00:44,330 it's automatically part of the top group, foreground processes. 13 00:00:44,330 --> 00:00:48,560 And it's extremely unlikely that our services process will be killed. 14 00:00:48,560 --> 00:00:53,151 To make our service a foreground service, all we need to do is call start 15 00:00:53,151 --> 00:00:57,911 foreground from our service and provide a notification along with an id. 16 00:00:57,911 --> 00:00:59,915 Let's see how to do that. 17 00:00:59,915 --> 00:01:03,461 Let's start mPlayerService by creating 18 00:01:03,461 --> 00:01:08,939 a notification.builder object at the top of OnStart command. 19 00:01:08,939 --> 00:01:16,452 Notification.Builder and let's call it notificationBuilder and 20 00:01:16,452 --> 00:01:21,374 set it equal to new Notification.Builder, 21 00:01:21,374 --> 00:01:25,010 and pass in this for the context. 22 00:01:26,950 --> 00:01:30,550 Next let's call setSmallIcon on our builder 23 00:01:33,400 --> 00:01:38,790 and pass in R.mipmap.ic_launcher 24 00:01:38,790 --> 00:01:44,310 to set our notifications icon to be the default icon included in our app. 25 00:01:44,310 --> 00:01:48,860 Then let's create a new notification, Notification, and 26 00:01:48,860 --> 00:01:52,130 we'll call it notification and 27 00:01:52,130 --> 00:01:57,430 set it equal to notificationBuilder.build. 28 00:01:57,430 --> 00:02:01,784 Next let's call startForeground and pass an 11 for 29 00:02:01,784 --> 00:02:06,535 the first parameter and our notification for the second. 30 00:02:06,535 --> 00:02:09,892 The first parameter is an id that we choose, 31 00:02:09,892 --> 00:02:14,358 we just need to not reuse notification ids in the same app. 32 00:02:14,358 --> 00:02:19,792 Since this is our only notification, we'll be fine just picking any random number, 33 00:02:19,792 --> 00:02:23,501 like 11, just don't pick zero, that's not allowed. 34 00:02:23,501 --> 00:02:29,281 Lastly, if the music is done playing we can stop running in the foreground. 35 00:02:29,281 --> 00:02:33,963 Let's call stopForeground right below stopSelf and 36 00:02:33,963 --> 00:02:39,900 pass in true to make sure that we remove the notification. 37 00:02:39,900 --> 00:02:44,680 And that's it, that's all you need to do to run your service in the foreground. 38 00:02:44,680 --> 00:02:46,980 If we were making a more finished product, 39 00:02:46,980 --> 00:02:51,940 we might want to add some buttons to our notification and update our app icon. 40 00:02:51,940 --> 00:02:54,525 But we're not, so let's go ahead and test the app and 41 00:02:54,525 --> 00:02:56,606 see what a foreground service looks like. 42 00:03:00,230 --> 00:03:02,980 But before we get to testing, 43 00:03:02,980 --> 00:03:08,157 it looks like there's an error with this call to build. 44 00:03:08,157 --> 00:03:12,412 It looks like this call requires API 16 but 45 00:03:12,412 --> 00:03:17,711 our current min is 15, down here at the bottom left. 46 00:03:17,711 --> 00:03:21,317 Let's open our app's build.gradle file to fix this. 47 00:03:27,165 --> 00:03:31,976 And set minSdkversion to 16, and then sync now. 48 00:03:34,655 --> 00:03:39,476 And if we go back to player service, no more errors. 49 00:03:39,476 --> 00:03:41,129 Let's get back to testing the app. 50 00:03:48,766 --> 00:03:52,410 And if we play the music, there's our notification. 51 00:03:54,200 --> 00:04:00,005 And if we quit the activity, the notification is still there. 52 00:04:00,005 --> 00:04:02,039 And if we wait for the song to end, 53 00:04:02,039 --> 00:04:07,055 we could even watch our notification disappear because the service was stopped, 54 00:04:07,055 --> 00:04:09,832 and it triggers the stop foreground method. 55 00:04:09,832 --> 00:04:11,432 Fantastic work. 56 00:04:11,432 --> 00:04:15,108 This is definitely some of the most difficult material in Android, 57 00:04:15,108 --> 00:04:19,040 and you should be extremely proud for finishing this course. 58 00:04:19,040 --> 00:04:20,920 Keep practicing and I'll see you soon.