8

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

rico_s
  • 221
  • 2
  • 6
  • is it a [local bound service](https://developer.android.com/guide/components/bound-services#Binder)? – pskink Nov 02 '18 at 13:07
  • @pskink: yes .. local bound service – rico_s Nov 02 '18 at 13:18
  • if so, you can create your `LiveData` objects there and your clients (those that implements `ServiceConnection`) can access them directly – pskink Nov 02 '18 at 13:20
  • @pskink: since this local bound service is a '3rd party sdk', i cant modify it, i need to consume its events via IEvenListener .. anyway, i need to somehow bind at all .. its q question about 'how to get the instance' – rico_s Nov 02 '18 at 13:20
  • honestly i dont get it: if its `IntentService` then you should call `startService` shouldnt you? so why is it bound also? do you have an interface for that binding? what does `Service#onBind` return? – pskink Nov 02 '18 at 15:26
  • 1
    @pskink: startService is a method of Context, but i do not have a Context in a repository .. the repository class is an annotated singleton (javax.inject.singleton), that is instanciated and can be injected to ViewModels by Dagger2 Dependency Injection ... thus, what I'm asking is a 'bridge' between a) Manifest.xml defined Services (IntentService), that can be started or bind by a context and b) a class that has no android context but is instanciated/managed by Dagger2 dependency injection framework .. in short: i simple can't call startService inside repository class without a context – rico_s Nov 02 '18 at 15:39
  • Hey @rico_s, were you able to solve this problem? I am struggling with exactly the same dilemma. – Seven Aug 20 '19 at 18:34
  • Do you have the possibility to add the context to your repo constructor? – msc87 Nov 23 '22 at 16:27

0 Answers0