2

It's simple example, but cann't understand. Music player. I have Activity with UI and functions (play, pause, stop ....). Me need do service which playing music in background. How service and activity can communicate. For example:

  1. in Activity we select song
  2. id song go to service with intent (with help putExtra)
  3. service -> play (song)
  4. we close activity and service work
  5. through time (song change) we open activity and he shows new current song.

How service "say" activity about changes? And how activity again "say" service about changes? (service we start so putExtra not suitable).

If have code examples show plz. Sorry for bad English.

MByD
  • 135,866
  • 28
  • 264
  • 277
Alex
  • 209
  • 1
  • 3
  • 9

2 Answers2

0

There are many examples of communication between services and activities on the internet.

This has some examples: How to have Android Service communicate with Activity

And so does this: Sending data from service to activity

Basically it looks like you can use an intent or just have a singleton service.

Community
  • 1
  • 1
James Cross
  • 7,799
  • 8
  • 24
  • 30
0

Also, specifically for a music player take a look at this project:

https://github.com/kreed/vanilla

Here is the service they use to play the music: https://github.com/kreed/vanilla/blob/master/src/org/kreed/vanilla/PlaybackService.java

It looks like they are using callback in the service somehow.

James Cross
  • 7,799
  • 8
  • 24
  • 30