1

I'm creating player app and i want to use foreground service for my media player. I need to update UI using livedata from service. I'm already read about using broadcast receiver but i want to use livedata.

  • Does this answer your question? [how to observe a live data inside service class](https://stackoverflow.com/questions/63899503/how-to-observe-a-live-data-inside-service-class) – Xid Nov 08 '22 at 03:04

3 Answers3

4

Suppose you use have a service with live data as follows

class MusicService: Service() {
    companion object {
        const val STAT_PLAY = "playing"
        const val STAT_PAUSE = "pause"
        val playerStateLiveData = MutableLiveData<String>()
    }
    /*
     * your code here to implement play, pause, etc
     * */
     private fun playAt(duration: Int) {
         /*play music player logic*/
         //update live data
         playerStateLiveData.postValue(STAT_PLAY)
     }
     private fun pause() {
         /*pause music player logic*/
         //update live data
         playerStateLiveData.postValue(STAT_PAUSE)
     }
}

And then on your activity, you can cast the MutableLiveData as LiveData to get updates

class MainActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        
        val liveData: LiveData<String> = MusicService.playerStateLiveData
        
        liveData.observe(this, { musicPlayerState ->
            //update main activity view based on state changed
        })
    }
}

Im suggesting you to use viewmodel to get mutable live data from service

Hendy
  • 88
  • 5
0

You could find your answer here:

https://stackoverflow.com/questions/63899503/how-to-observe-a-live-data-inside-service-class
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Aug 31 '21 at 09:19
0

You could use the singleton pattern inside the service to initialize an LiveData object and observe from your activity

Let's say your service is TestingService with the following code:

class TestingService : LifecycleService() {
    
    companion object {
        val testingNumber = MutableLiveData<Int>()
    }

    override fun onCreate() {
        super.onCreate()
        initValues()
    }

    private fun initValues() {
        testingNumber.postValue(0)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        intent?.let {
            when (it.action) {
                ACTION_START_SERVICE -> {
                    // Starting service
                    testingNumber.postValue(2)
                    startForegroundService()
                }
                ACTION_STOP_SERVICE -> {
                    // Stopping service
                    stopService()
                }
            }
        }
        return super.onStartCommand(intent, flags, startId)
    }
}

and you can observe the LiveData from the activity in this way:

 class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setObservers()
    }

    private fun setObservers() {
        TestingService.testingNumber.observe(this, Observer {
            updateUi(it)
        })
    }
}
Hayssam Soussi
  • 903
  • 7
  • 16