1

I am building a music player in android for which I am using bound services to create mediaplayer and do all the background operations like playing media and pause etc like this.

All the UI components in mediaplayer are implemented in activity and I need to communicate events like mediaplayer is ready, media is playing, paused etc from service to activity to make changes in UI accordingly. What would be best way to do that?

Broadcasts from service to activity for various states is one way to implement it.

pratsJ
  • 3,369
  • 3
  • 22
  • 41
  • 1
    EventBus from green robots. https://github.com/greenrobot/EventBus – Raghunandan Aug 25 '14 at 18:09
  • A simple way is have the Service take a reference to an interface that the Activity implements when it binds. Just make sure to have Service let go of it when Activity is destroyed. – Steve M Aug 25 '14 at 18:10
  • 1
    If you want to used broadcasts you can create a `BroadcastReceiver` in your activity to catch broadcasts from your service. Otherwise as @svenoaks suggests you can interact with your activity directly. – Rarw Aug 25 '14 at 18:16
  • 1
    I managed to use binder interface of boundservices to communicate from activities to services and localBroadcastManager to send broadcasts to activites from service. – pratsJ Sep 15 '14 at 16:57
  • You can use Pending Intent,Event Bus,Messenger or Broadcast Intents for communicating events from service to activity .Check out my [blog post](https://androidlearnersite.wordpress.com/2018/02/23/communicating-from-services/) which covers all these approaches. – Android Developer Feb 28 '18 at 09:06

0 Answers0