How to send data from service to activity?

拥有回忆 提交于 2019-12-31 04:07:28

问题


I'm beginner in Android development. I try to create kind of MusicPlayer, which uses Service for playing music. When I click buttons in MainActivity, I call startService with Intent, which includes signal for action, such as play, pause, stop, etc... But I want to send data back from service to activity. For example, when current track has stopped and begin to play the next one, MainActivity got to receive track name and author name. Or when last track in the playlist is stopped, button "Pause" on the Activity got to changed to "Play". I need to send data as map. Like "title: value", "playing: false". So, what I tried. At first PendingIntent. I create it in the Activity and send by startService to the Service. I need to specify request code in PendingIntent, sting action in Intent for this PendingIntent. I don't know, how to use it. Then I tried bindService. But Service send data only if Activity request it. Another examples I didn't find.

Which way is most simple to send data from Service to Activity?


回答1:


Use a messenger to send the Message from the Service on onHandleIntent() method, then Recieve it with A handler...see this tutorial http://www.vogella.com/articles/AndroidServices/article.html



来源:https://stackoverflow.com/questions/17909377/how-to-send-data-from-service-to-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!