title: Send Data from Service To Activity Android
body: <p>I am building a music player that uses a service for playback. I have an Activity UI that controls (play, pause, next, ...) the service. 
  I want to update the UI from my service when the next or previous button is pressed. I thought of passing an integer value. Here is my code:</p>
  
  <p>I have used a messenger for communication. The code in my service looks like:</p>
  
  <pre><code>enum Event {
      NextSongChanged, CurrentPlayingSong
  };
  
  public synchronized void registerHandler(Messenger m) {
      clients.add(m);
  }
  
  
  private void emit(Event e) {
      for (Messenger m : clients) {
          try {
              m.send(Message.obtain(null, e.ordinal()));
          } catch (RemoteException exception) {
              /* The client must've died */
              clients.remove(m);
          }
      }
  }
  </code></pre>
  
  <p>My method that I am calling on click of next button:</p>
  
  <pre><code>public synchronized void playnext() {
      reset();
      if(songIndex &lt;  (songsList.size() - 1)) {
          songIndex += 1;
          playSong(songIndex);
      } else {
          songIndex = 0;
          playSong(songIndex);
      }
      emit(Event.NextSongChanged);
  }
  </code></pre>
  
  <p>As soon as I fire the NextSongChanged event I want to pass the "songIndex" variable into my activity. Any idea on how to achieve this?</p>
  
  <p>My Activity code to handle the event:</p>
  
  <pre><code>private Messenger playerMessenger = new Messenger(new Handler() {
      @Override
      public void handleMessage(Message msg) {
          switch (MyService.Event.values()[msg.what]) {
              case NextSongChanged:
                  //String songTitle = songsList.get(currentSongIndex+1).get("songTitle");
                  //currentSongIndex += 1;
                  //songTitleLabel.setText(songTitle);
                  //updatePlaying();
                  break;
  
              case CurrentPlayingSong:
                  break;
          }
      }
  });
  </code></pre>