Reputation: 41
I have a fragment and a service that is called by the fragment.
The fragment contains a gauge, and the service uses a device's microphone to meausure sound. I would like the gauge to reflect the sound.
Is there a way for me to transfer the sound value from the service to the fragment?
I have tried to send the gauge object from fragment to the service through a parcelable, but because the gauge is from a custom library, I cannot parcelize or serialize it. I have also tried to create a custom class with just an int attribute, sending the object from the fragment to the service, hoping that updating the int attribute in the service would update the int in the fragment, but it didnt work out.
I want this update to be happening at each second of the measurement, so sending the information OnDestroy of the service does not work for me.
Do you have any suggestions on how I could do this? Thanks
Upvotes: 0
Views: 39
Reputation: 46
For Communication between service and fragment, you have to create boundService. Implementing TimerTask in service for each second will solve problem of updating value. Note: we can use liveData of sound value in service and register its observer in fragment so whenever timertask update value in livedata object it will reflect in fragment.
Upvotes: 1