title: Android Architecture Components: ViewModel/Repository vs bind to Service/IntentService
body: <p>I want to implement/refactor an app to the Android Architecture Components concept, see <a href="https://developer.android.com/jetpack/docs/guide" rel="noreferrer">https://developer.android.com/jetpack/docs/guide</a></p>
  
  <p>In this topic <a href="https://stackoverflow.com/q/46682425/6251815">Android Architecture Components ViewModel - communication with Service/IntentService</a>, I found a very good answer to the architectural issue, see <a href="https://stackoverflow.com/a/46736146/6251815">https://stackoverflow.com/a/46736146/6251815</a> </p>
  
  <p>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. </p>
  
  <p><strong>What is my situation?</strong></p>
  
  <p>I need to have a boundService (see <a href="https://developer.android.com/guide/components/bound-services" rel="noreferrer">https://developer.android.com/guide/components/bound-services</a>), 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.</p>
  
  <p><strong>What do I want to do?</strong></p>
  
  <p>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 <a href="https://developer.android.com/jetpack/docs/guide#fetch-data" rel="noreferrer">https://developer.android.com/jetpack/docs/guide#fetch-data</a></p>
  
  <p>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'.</p>
  
  <p>Unfortunately, we typically need the following code the bind a background service to an activity:</p>
  
  <pre><code>Intent csIntent = new Intent(XXX, ThirdPartyService.class);
  YYY.bindService(csIntent, &lt;instance of ServiceConnection&gt;, Context.BIND_AUTO_CREATE);
  </code></pre>
  
  <p>where XXX and YYY are context and the activity (but both should not appear in repository!)</p>
  
  <p><strong>What is the problem?</strong></p>
  
  <p>How this concept of activity-focused background service binding must be modified accourding to <a href="https://developer.android.com/guide/components/bound-services" rel="noreferrer">https://developer.android.com/guide/components/bound-services</a>, if i want to access this background service from a 'architecture component repository' that is implemented as dagger2 @Singleton according to <a href="https://developer.android.com/jetpack/docs/guide#manage-dependencies" rel="noreferrer">https://developer.android.com/jetpack/docs/guide#manage-dependencies</a></p>
  
  <p>Unfortunately, the only semi-offical document i found to this problem state that a demo 'should' be made (but ticket was closed): <a href="https://github.com/googlesamples/android-architecture-components/issues/20" rel="noreferrer">https://github.com/googlesamples/android-architecture-components/issues/20</a></p>
  
  <p>Thanks for any hints how to merge both concepts</p>