Question

I've created a music service for my application (a music player), and after making some tests I'm observing a behaviour that's causing me a headache as I don't know how to solve it.

For testing purposes, I've modified the service so, as soon as it's started, it plays a specific mp3 file from my sd card. Also, I've modified the application so the first activity is started, it starts the service, and then it calls "finish()".

Ok, so... I launch the application, and the first activity starts, my service starts and plays the music, the activity finishes and the application is closed and... the music is stopped and after some seconds the service is restarted (I'm using the START_STICKY flag, so I suppose that's normal).

I don't want the music to be stopped when I close the application, or in another words, I don't want the service to be stopped (and then restarted because it's been stopped) when my application is closed.

Right now, to control the music service, I start the service and then I bind to it so I can call the service functions I've defined in an interface.

Any ideas?

Thanks!

EDIT:

This is an example of what my application and service do (in the tests I'm doing).

Activity:

public class FirstActivity extends SherlockFragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        startService(new Intent(this, MyService.class));

        ...
        // Here is a postDelayed that will run after 2 seconds and call finish()
    }
}

Service:

public class MyService extends Service {
    private MediaPlayer mPlayer;

    @Override
    public void onCreate() {
        mPlayer = new MusicPlayer(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Here is the code that plays the music using MediaPlayer

        return START_STICKY;
    }
}
Was it helpful?

Solution

do not forget to put that the service is remote in your manifest application tag:

<service
  android:name="somepackage.PlayerService"
  android:label="Player Service"
  android:process=":remote"/>

Note, that the remote name can be something else, so you can have more than one services that are not bound to the main application process

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top