I'm trying to make an app that plays audio with the MediaBrowserService but also use the new Architecture Components to structure the whole app. I have use MediaBrowserService before for another app( https://github.com/willhwongwork/PodCast ) but in that app I did not use the OnGetRoot() and OnGetChildren() methods to load the data I just load data using loader or asynctask from network(and there's no architecture in that app).
So now if I want to use LiveData and ViewModel how should I structure the code? Should I use them in the MediaBrowserService and through the OnGetRoot() and OnGetChildren() methods provide the data to the UI?
Unlike AppCompatActivity
or support Fragment
which are LifecycleOwner
due to which we can use ViewModelProviders
and observe
of LiveData
, the Service
doesn't implement the LifecycleOwner
interface.
That said, there is LifecycleService which implements the LifecycleOwner
interface. I don't think the MediaBrowserService
implements the LifecycleOwner
hence you'll need to use ServiceLifecycleDispatcher and create a custom MediaBrowserService
.
Note: Kindly also take a look at what Florina says about Service with Lifecycle: https://github.com/googlesamples/android-architecture-components/issues/137
You can use onLoadChildren() method to load chunks of data, using the new Paging library , which is part of the Android Architecture Components. I wrote a post about it where I use a cursor to fetch MediaItems from the MediaStore, but the same can be done with LiveData as well
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.