Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Messengers and Handlers: Activity11:48 with Ben Deitch
In this video we’ll finish refactoring our app to use a Messenger so our app can take advantage of running in multiple Processes!
Switching our focus over to main activity, 0:00 let's start by deleting the mPlayerService field. 0:03 This makes it easy to see all the places where we need to make changes. 0:10 Next, let's create a new class for activities handler, and 0:14 let's call it activityHandler. 0:18 Then let's make it extends handler and let's override the handle message method. 0:25 Control O, handle message. 0:32 Let's get rid of the call to super. 0:37 The only way for this method to be called is if our activities handler 0:41 is hearing back from our services handler about whether music is playing or not. 0:45 And remember that if music is not playing, message.arg1 should be zero. 0:50 So if message.arg1 is zero, and 0:56 let's add a comment that music is not playing. 1:01 Then let's add an else if for when arg1 is one. 1:12 And let's add a comment for this, as well. 1:21 Music is playing. 1:25 Back in the first IF statement, let's add two more comments for 1:27 what we're trying to accomplish. 1:31 Play the music and change play Button to say "Pause". 1:34 Then let's copy these comments into the L sieve 1:44 And update them to say Pause the music, and change play button to say play. 1:52 To play the music, 2:00 we need to send a message with an ARG one of zero to our services handler. 2:01 Below our play the music comment, let's create a new message, message message. 2:07 Equals Message.obtain and 2:15 said it's arg1 to 0, message.arg1 = 0. 2:17 Then we need to use our services messenger to send the message. 2:22 We could grab this messenger from the activity, but wouldn't it be nice if it 2:28 was just the reply to of the message we received back in player handler? 2:32 And case two below, where we said arg1 let's 2:41 set our message's reply to to our services messenger. 2:45 But first, we'll need to make our services messenger public so 2:53 we can access it from this class over employer service, 2:59 set our messenger to public, then back in player handler. 3:04 Let's set that messenger as our messages reply to property, equals 3:10 employer service dot m messenger. 3:15 Now back in our activity handler, we can call 3:20 message, msg.reply to get the messenger of our service, 3:27 .send and pass in our message. 3:33 And let's use alt+enter to handle the exception. 3:39 Then let's copy and paste this code below our pause the music comment, 3:43 and change our one to be equal to one. 3:51 Now we just need a way to change the text of our play button from our handler, 3:57 just like we did with our services handler. 4:02 Let's create a field from main activity, private main 4:04 activity in main activity and 4:10 lets add a constructor that takes the main activity as the parameter. 4:14 Public, activity handler, main activity, which we'll call main activity, 4:18 and inside, let's set our field to be equal to the pastan parameter. 4:27 mMainActivity equals mainActivity. 4:33 Next, let's add a public method to mainActivity 4:36 to let us change the text of our play button. 4:40 Let's add it below on create and call it change play button text, 4:43 public void change play button text, 4:49 and it takes in a string, which we'll call text. 4:58 And inside this method, let's update the text of our play button, 5:04 mPlayButton.setText, and pass in our text parameter. 5:07 Finally, back in our activity handler, let's use this method to update our text 5:15 So below the change play button to say pause comment, we'll write 5:24 mMainActivity dot changePlayButtonText and we'll pass in Pause. 5:29 And below this comment we'll type mMainActivity dot changePlayButtonText and 5:37 pass in Play. 5:43 All right, we're done with our activity handler and 5:46 it's time to finish up in main activity. 5:49 First, let's get rid of everything in red. 5:52 So everything below mBound and on service connected, and 5:56 everything except our call to start service inside the mBound if statement And 6:00 our play buttons OnClickListener And 6:06 then I'll highlight the call to startService and 6:15 use shift tab to align it. 6:19 Then let's create a new field for the messenger we receive from our service. 6:23 And let's call it mServiceMessenger. 6:28 So right below where we declare our play button, 6:32 private Messenger_mServiceMessenger;, And Alt+Enter to import Messenger. 6:36 Next, inside onServiceConnected. 6:46 Let's set mServiceMessenger equal to a new Messenger, 6:49 mServiceMessenger equals new Messenger and let's pass in the binder parameter. 6:55 Then, we need to send a message to our service to see if music is playing, so 7:04 we can update the UI accordingly. 7:09 Let's create a new message. 7:12 Message message = Message.obtain and 7:13 give it an arg1 of 2 to signify is playing message.arg1 = 2. 7:20 And if we want to hear back, 7:27 we need to provide our own messenger as the reply to parameter. 7:29 Let's create a new field for our activities own messenger and 7:34 name it mActivityMessenger. 7:39 Private Messenger mActivityMessenger and 7:41 say = new Messenger(). 7:47 Then let's pass in a new instance of our activity handler as a parameter. 7:53 And that activity handler then requires an instance of main activity which we can 8:00 pass in using the this keyword and add a semi colon. 8:05 Then let's set the reply to of our message to be our new ActivityMessenger. 8:08 Message.replyTo = mActivityMessenger;. 8:16 And let's use our services messenger to send the message to our service. 8:22 mServiceMessenger.send and pass in the message. 8:26 And use alt enter to handle the exception. 8:33 As the pen ultimate step, let's copy all of this message code and 8:37 paste it into the void we left in our on-click method. 8:41 If we run the app now, music will start playing immediately because we don't have 8:53 a way to update our button's text without playing or pausing music. 8:58 No matter how we get to the activity handler, it's either going to play or 9:02 pause music. 9:07 To fix this, let's pass in an extra argument when we 9:09 only want to update our play buttons text and on service connected, 9:12 let's add another argument or two and set it equal to one. 9:18 Message.arg 2 equals 1. 9:24 Then mplayer handler. 9:29 If we're inside the is playing case, and the provided message has an arg2 of 1, 9:33 then let's also give our new message an arg2 of 1. 9:38 So, right below where we set arg1 to is playing, let's write, 9:43 if msg.arg2 == 1. 9:48 And if it does, let's set message.arg2 = 1. 9:51 Finally, an activity handler we need to handle when arg2 is one. 10:01 So if music is not playing and arg2 is one, which means we're only concerned 10:08 with the button being labeled correctly, we should change the button to say Play. 10:13 Otherwise, we should keep doing the same thing. 10:19 So below the music is not playing comment. 10:23 Let's type if msg.arg2 -- 1. 10:26 And if it is we need to update our play button to say play. 10:31 So mMainActivity.changePlayButtonText 10:36 to play, else we will keep doing what we were doing before. 10:43 And for the second if statement, if music is playing and 10:49 we're only concerned with updating the button's text, then we should change 10:54 the button to say pause Else we should keep doing the same thing. 10:57 So if message.org2 11:01 is one, then we'll change the play button to say pause in main 11:07 activity that change play button text to pause and 11:12 else we'll keep doing the same thing, and there we go. 11:17 Now, let's test the app and see if it works. 11:22 Play. 11:32 [MUSIC] 11:33 Pause play again, with the app, and open the app again, 11:35 and it says pause, because the music's still playing. 11:41 Awesome. 11:47
You need to sign up for Treehouse in order to download course files.Sign up