Pass Service From one Activity to Another
<p>how do I pass a service from one activity to another? I have a music player (Activity1) that displays a list of songs, and when you click on it, it starts the service within Activity1. I have a button that users can click that will open Activity2. </p>

<p>So what is the best way for me to pass the service from Activity1 to Activity2. If the service is started in Activity1, then Activity2 should continue playing. If the service is not started in Activity1, then Activity2 should start the service before using it.</p>

<p>Thanks.</p>

<p>Here's some sample code, the MusicService is a class that extends the service class.</p>

<pre><code> public class Activity1 extends AppCompatActivity {

 private MusicService serviceMusic;  

private ServiceConnection musicConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        MusicService.PlayerBinder binder = (MusicService.PlayerBinder) service;
        //get service
        serviceMusic = binder.getService();
        serviceMusic.setSongList(songList);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
};
</code></pre>

<p>}</p>