1 00:00:00,860 --> 00:00:04,320 If we want a working music player, we'll need a way to call the music 2 00:00:04,320 --> 00:00:09,520 playback methods in our service, when we tap buttons in our activity. 3 00:00:09,520 --> 00:00:12,070 So how can we pass our service to our activity? 4 00:00:13,730 --> 00:00:17,370 Let's start in main activity and talk about what we'll need to be coding. 5 00:00:18,550 --> 00:00:23,370 First, we should decide where in our activity to buy into the service and 6 00:00:23,370 --> 00:00:24,140 where to unbind. 7 00:00:25,860 --> 00:00:30,440 Users are only able to interact with our app when it's in the foreground. 8 00:00:30,440 --> 00:00:34,350 So we could probably get away with binding an onResume and 9 00:00:34,350 --> 00:00:39,750 unbinding an onPause, but that could be a lot of binding just for a little pop up. 10 00:00:41,000 --> 00:00:46,950 Instead, we'll do the binding and onStart and unbinding in onStop. 11 00:00:46,950 --> 00:00:51,130 This way if our app is visible, we'll be bound to our service and 12 00:00:51,130 --> 00:00:52,490 able to use our music player. 13 00:00:53,500 --> 00:00:58,650 We could also bind an onCreate an unbind and onDestroy either one works. 14 00:01:00,010 --> 00:01:01,460 All right let's get started. 15 00:01:02,490 --> 00:01:06,990 At the bottom of main activity, let's add some space and 16 00:01:06,990 --> 00:01:08,498 override the onStart method. 17 00:01:08,498 --> 00:01:14,313 Control O onStart and make sure to pick the right one. 18 00:01:17,193 --> 00:01:22,206 While we're at it, let's also override the onStop method below onStart. 19 00:01:27,177 --> 00:01:31,088 When we bind it to a service, we use the bind a service method, 20 00:01:31,088 --> 00:01:34,940 and just like the start service method, it takes an intent. 21 00:01:35,990 --> 00:01:41,660 Back onStart, below the call to super, let's create an intent for 22 00:01:41,660 --> 00:01:46,131 our player service class, intent, intent 23 00:01:46,131 --> 00:01:51,140 equals new intent pass in the context 24 00:01:51,140 --> 00:01:56,380 using the this keyword and pass end play service dot class. 25 00:01:59,850 --> 00:02:04,785 On the next line, let's bind to our service by typing bindService. 26 00:02:07,260 --> 00:02:09,200 And note that we need three arguments. 27 00:02:10,210 --> 00:02:16,230 The intent for our service, something called a ServiceConnection, and any flags. 28 00:02:17,900 --> 00:02:23,702 Let's pass and our intent for the first parameter skip the ServiceConnection for 29 00:02:23,702 --> 00:02:30,460 now and pass in context.BIND_AUTO_CREATE for 30 00:02:30,460 --> 00:02:35,660 the third parameter, to have our service automatically created when we bind to it. 31 00:02:37,880 --> 00:02:40,400 Okay, let's get back to that service connection. 32 00:02:41,590 --> 00:02:45,030 Binding and unbinding don't happen immediately. 33 00:02:45,030 --> 00:02:47,990 The service connection is what we used to tell us, 34 00:02:47,990 --> 00:02:52,250 when we're successfully connected and also when we've been disconnected. 35 00:02:53,620 --> 00:02:59,250 When we call bindService, Android starts building a connection between our activity 36 00:02:59,250 --> 00:03:04,230 and our service and when that connection is finally established, 37 00:03:04,230 --> 00:03:08,700 Android will call the onService connected method of our service connection that we 38 00:03:08,700 --> 00:03:14,170 provide as a parameter And if we ever get disconnected from our service 39 00:03:14,170 --> 00:03:18,370 Android will let us know why calling the onServiceDisconnected method. 40 00:03:20,240 --> 00:03:24,200 In addition to binding, we'll also need to use our service connection when we 41 00:03:24,200 --> 00:03:26,060 unbind and the onStop method. 42 00:03:27,930 --> 00:03:31,320 So if we need our service connection and onStart and 43 00:03:31,320 --> 00:03:34,320 onStop, we'll need to create it as a field. 44 00:03:35,510 --> 00:03:41,040 At the top of our class below our buttons, let's add a field for 45 00:03:41,040 --> 00:03:46,836 our service connection, private ServiceConnection, 46 00:03:46,836 --> 00:03:50,500 mService connection. 47 00:03:50,500 --> 00:03:53,720 We still need to override the onService connected and 48 00:03:53,720 --> 00:03:56,480 the onService disconnected methods of our service connection. 49 00:03:57,490 --> 00:04:01,506 We could create our own class that implements service connection, 50 00:04:01,506 --> 00:04:05,812 override those two methods, and then create a new instance of that class 51 00:04:08,743 --> 00:04:11,390 Or we could just use an anonymous class. 52 00:04:17,650 --> 00:04:21,610 If you've never used anonymous classes before, you're lying. 53 00:04:21,610 --> 00:04:22,630 There's one right here. 54 00:04:24,960 --> 00:04:28,700 An anonymous class is pretty much just a way to extend a class 55 00:04:28,700 --> 00:04:31,880 without having to create a whole new class. 56 00:04:31,880 --> 00:04:34,650 It's a way for us to add functionality to an object 57 00:04:34,650 --> 00:04:37,100 without needing to create a new type of object. 58 00:04:38,940 --> 00:04:45,746 Let's set our ServiceConnection to be = to a new anonymous ServiceConnection class. 59 00:04:45,746 --> 00:04:50,930 So = new ServiceConnection. 60 00:04:50,930 --> 00:04:52,430 Hit enter and there we go. 61 00:04:53,600 --> 00:04:58,338 Back in the on start method let's pass on our new ServiceConnection field as 62 00:04:58,338 --> 00:05:00,794 the second parameter to bind service. 63 00:05:06,193 --> 00:05:12,460 And an onStop below the call to super, let's call the unbindService method. 64 00:05:15,660 --> 00:05:19,510 And, as promised, it takes a service connection parameter. 65 00:05:19,510 --> 00:05:20,486 Let's pass that in. 66 00:05:23,664 --> 00:05:25,430 We're almost done. 67 00:05:25,430 --> 00:05:31,550 But unfortunately, unbindService can throw an error if the service isn't bound. 68 00:05:31,550 --> 00:05:34,870 One way this can happen is if our call to bind the service 69 00:05:34,870 --> 00:05:37,440 wasn't successful in the first place. 70 00:05:37,440 --> 00:05:41,350 If we didn't bind to our service, how can we expect to unbind from it? 71 00:05:42,590 --> 00:05:47,520 We'll fix this by adding a boolean field named mBound to track whether or 72 00:05:47,520 --> 00:05:49,620 not we are bound to the service. 73 00:05:49,620 --> 00:05:55,022 Below our key declaration, Let's 74 00:05:55,022 --> 00:06:00,910 add this new field private boolean mBound. 75 00:06:01,910 --> 00:06:03,440 And let's set it = false. 76 00:06:06,190 --> 00:06:10,920 Now, we need to update our inbound variable when our service is bound and 77 00:06:10,920 --> 00:06:11,950 when it's unbound. 78 00:06:12,980 --> 00:06:14,840 But how can we tell when our service is bound. 79 00:06:16,250 --> 00:06:17,100 That's right! 80 00:06:17,100 --> 00:06:20,060 The onServiceConnected method of our service connection. 81 00:06:21,470 --> 00:06:23,350 And when our services unbound or 82 00:06:23,350 --> 00:06:27,120 disconnected, the onServiceDisconnected method will be called. 83 00:06:28,780 --> 00:06:35,821 So when onServiceConnected, let set mBound = to true and 84 00:06:35,821 --> 00:06:43,590 then onServiceDisconnected Let's set mBound equal to false. 85 00:06:46,270 --> 00:06:51,517 Last but not least, let's make sure we're only calling 86 00:06:51,517 --> 00:06:56,327 unbind service if our service is actually bound, so 87 00:06:56,327 --> 00:07:01,570 if mBound And add the other bracket at the bottom. 88 00:07:03,830 --> 00:07:08,808 Then right below onbindService, let's set mBound to false. 89 00:07:13,682 --> 00:07:19,200 On service disconnected is only called when something unexpected happens. 90 00:07:19,200 --> 00:07:24,890 So calling unbind a service won't result in a call to on service disconnected, 91 00:07:24,890 --> 00:07:26,520 which is why we're responsible for 92 00:07:26,520 --> 00:07:29,370 updating mBound after calling unbind service. 93 00:07:31,100 --> 00:07:33,760 We're almost done binding to our service. 94 00:07:33,760 --> 00:07:38,320 Can you hear the music yet, me neither, but 95 00:07:38,320 --> 00:07:39,970 I think we might hear some in this next video.