Reputation: 2793
I am currently creating a music player in Android and I've gotten to the point where everything works rather well. The next step for me is to create a service that allows the music to play in the background even if the user has closed the activity. I have read the Android documents on services and I understand the theory behind them. What I don't understand however is how to reference the activity objects/variables from the service.
As far as I understand, I will use the activity to simply instantiate all the views and the service will hand the MediaPlayer
object and thus be responsible for updating views such as the Seek bar, song name, album cover, etc.
So how exactly do I go about referencing the activity views from my service?
NOTE: I am making my application to work with Android API 16 and above.
Upvotes: 2
Views: 229
Reputation: 1006819
What I don't understand however is how to reference the activity objects/variables from the service.
You don't.
I will use the activity to simply instantiate all the views
Correct.
the service will hand the MediaPlayer object
Correct.
and thus be responsible for updating views such as the Seek bar, song name, album cover, etc.
Incorrect. Your service is responsible for letting your UI know -- if you even have a UI -- about state changes in the playback. The service does not directly update the UI, any more than MySQL directly updates the DOM in a Web browser.
I recommend a message bus model, where the service sends out messages that UI components (e.g., an activity) can listen for, if desired, and use to update their UI when relevant (e.g., when in the foreground). Implementations of this include:
Upvotes: 4