user3062224
user3062224

Reputation: 21

MediaPlayer using services

Can someone enlighten me about the android services? I'm really having a hard time understanding it. I wanted to make a music player application. Just a simple application that shows a listview of songs and has the usual buttons and functionalities of a music player. However, in creating this app, I'm thinking of what kind of service to use. I'm thinking of using bound services but I'm confused if my decision is correct. I've read about the services but unfortunately, I could not understand it properly. I mean I know what it does but when it comes to implementing the codes, that's the time when my confusion starts. Then there's a time I read an information about implementing it as a foreground service rather than a background. Now that adds up my confusion. What will i really use? Which is which? please help me and if possible, let me understand about implementing the codes.

Upvotes: 0

Views: 63

Answers (1)

Halim Qarroum
Halim Qarroum

Reputation: 14093

One of the difference between a Service started with startService and one started with bindServiceis how you are going to manage its lifecycle.

Because you have to call unbindService after having called startService, the lifecycle of your service is intimately bound to the lifecycle of your bound component (often, an activity). As such, when you'll call unbindService, your service will be destroyed if it has no other ServiceConnection bound to it.

The question here is about knowing whether you would like to have your service being dependant on the lifecycle of your activity, or not. If you want to keep it around even if the user leaves your app, than use startService and implement the associated onStartCommand callback in the service.

EDIT : A good example of this can be found in the samples provided with the Android SDK. Take a look at the RandomMusicPlayer, it implements a MusicService that might be what you want for your app, from what I've understood.

Upvotes: 1

Related Questions