问题
I want to implement/refactor an app to the Android Architecture Components concept, see https://developer.android.com/jetpack/docs/guide
In this topic Android Architecture Components ViewModel - communication with Service/IntentService, I found a very good answer to the architectural issue, see https://stackoverflow.com/a/46736146/6251815
But i want to ask, how to bind the service from a repository, because we have neither context nor activity here. To be clear on that, the question is about how to merge both concepts.
What is my situation?
I need to have a boundService (see https://developer.android.com/guide/components/bound-services), that comes from a third party as a library (lets call this '3rd party SDK'). This '3rd party SDK' will do some async stuff on bluetooth connection to some external hardware, thus it runs as a more or less permanent background service. However, its implemented as service (intentservice, thus an activity can bind to) and we must receive events by implementing a custom event listener interface.
What do I want to do?
I would like to use the Architecture Components as well. I defined the View and ViewModel and I would like to use a repository as 'Dagger2 Singleton' that provide data form local storage as well as web service calls, see https://developer.android.com/jetpack/docs/guide#fetch-data
My first intention was, that I could handle the '3rd party SDK' also as some kind of async quasi-remote data source and thus, the repository should also do the binding to that '3rd party SDK'.
Unfortunately, we typically need the following code the bind a background service to an activity:
Intent csIntent = new Intent(XXX, ThirdPartyService.class);
YYY.bindService(csIntent, <instance of ServiceConnection>, Context.BIND_AUTO_CREATE);
where XXX and YYY are context and the activity (but both should not appear in repository!)
What is the problem?
How this concept of activity-focused background service binding must be modified accourding to https://developer.android.com/guide/components/bound-services, if i want to access this background service from a 'architecture component repository' that is implemented as dagger2 @Singleton according to https://developer.android.com/jetpack/docs/guide#manage-dependencies
Unfortunately, the only semi-offical document i found to this problem state that a demo 'should' be made (but ticket was closed): https://github.com/googlesamples/android-architecture-components/issues/20
Thanks for any hints how to merge both concepts
来源:https://stackoverflow.com/questions/53118933/android-architecture-components-viewmodel-repository-vs-bind-to-service-intents