Messengers and Handlers: Service5:32 with Ben Deitch
In this video we’ll refactor our Service to use a Messenger instead of extending the Binder class!
Let's start with PlayerService. 0:00 Since we'll now be using a messenger instead of a class that extends binder, 0:03 let's go ahead and delete our localBinder class and our mBinder field. 0:08 [BLANK] 0:12 Next, we need to create a handler for our service. 0:22 Let's create a new class named 0:25 PlayerHandler, and make it extend Handler. 0:30 We're going to need a reference to our player service inside this handle. 0:39 A good way to make sure we have this reference is to require it and 0:44 the constructor. 0:47 Let's create a constructor for our PlayerHandler class and 0:50 make it take a player service as a parameter 0:53 public_PlayerHandler takes in a PlayerService. 0:58 Brackets. 1:08 Then, let's create a new PlayerService field and 1:10 set this field in our constructor. 1:13 private PlayerService_mPlayerService. 1:15 mPlayerService = playerService. 1:23 All right, now that we've got our playerService, 1:30 let's override the handle message method and write our handler. 1:34 Control O handleMessage and there we go. 1:40 Let's get rid of the call to super and 1:46 then add a switch statement to switch on our messages ongoing property. 1:49 And brackets. 1:58 Next, let's add a case for when message.arg1 is 0, 2:00 case 0: and I'll add my break at the end. 2:06 Earlier, I mentioned that we could use 0 to mean play. 2:11 So let's do that. 2:15 Let's add a comment to the right of case 0 that says Play 2:17 to help us remember what the number stands for. 2:22 Then let's call our services play method mPlayerService.play. 2:25 On to the next case. 2:31 Let's add a case one and a comment that says pause. 2:36 Then let's call our services pause method. 2:43 And mPlayerService.pause. 2:45 And add a break statement. 2:49 Next up, case 2. 2:51 And a comment that says isPlaying. 2:56 This is the tricky one. 3:00 Let's start by getting isPlaying as a 1 or a 0 instead of true or false. 3:02 This way, we can pass it as an argument with our message. 3:08 Let's create a new int named isPlaying and 3:12 set it = mPlayerService.isPlaying()?1 3:17 : 0; If you've never seen this notation before, 3:22 here's what it means. 3:28 If what's before the question mark is true, 3:31 return the first value 1. 3:36 And if it's fals,e return the second 0. 3:39 It's just a shortcut so we can get around using an if statement. 3:43 Now that we've got isPlaying as an integer, let's create a new message and 3:48 attach this integer as arg 1. 3:52 Message. 3:55 message = Message.obtain(). 3:57 And then message.arg1 = isPlaying. 4:01 Then let's send this message to our activity using the messenger that 4:06 we'll provide and the replyTo property of our message. 4:11 So using the message that we get from handle msg.replyTo, 4:16 which gives us the messenger attached to this message.send and pass in the message. 4:22 Lastly, use alt enter to handle the potential exception And 4:31 add a break statement. 4:37 Nice. 4:39 Now that we've got our handler for PlayerService, 4:40 let's go back to PlayerService and create a new field for our messenger. 4:43 private messenger. 4:52 mMessenger and set it = a new Messenger which 4:57 takes in either a handler or an AI binder. 5:02 So let's pass and a new instance of our playerHandler 5:08 which takes in a playerService. 5:15 So let's pass in this and then add a semicolon. 5:17 Finally in onBind let's return aMessenger.getBinder. 5:22
You need to sign up for Treehouse in order to download course files.Sign up