Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Need help understanding service
So I went through all the code for the lecture and the lectures themselves but I haven't understood how the PlayerHandler gets the different values of 0,1,2 for Message.arg1 if from the main acitvity we only ever pass 2 for arg1 . I tried debugging the code but still couldn't identify it. How does the Player Handler get different args from msg if ever only passed 2 to it.Is the ActivityHandler responsible,if yes then how ? it only ever replies with the same arg1 it got. so how does passing only 2 on button press pause or play the music. I tried debuging and noticed that the PlayerHandler onHandleMessage gets called twice once with 2 and again with button press args. so where does the second call come from. I went through the course again and even checked the code from site still no clarification. Also if in each onHandleMessage the replyTo is called then won't the acitivity and service keep constantly passing messages to each other. Please explain Ben Deitch .
Ben DeitchTreehouse Teacher
You're correct that the ActivityHandler is responsible. What happens is:
- We click the Button which sends a Message to our PlayerHandler with arg1 = 2.
- This puts us in 'case 2:' where we create a new Message and give it an arg1 of 0 or 1 (depending on if music is playing or not).
- Then we send this new Message to the ActivityHandler, which updates the Button text. And, if the music state needs to be changed, it sends another different new Message (with arg1 of 0 or 1) back to PlayerHander which then plays or pauses the music.
Tonnie FanadezUX Design Techdegree Graduate 22,790 Points
Thanks guys, this discussion has clarified how everything is working.
I'm still don't understand how this works.