views:

361

answers:

2

I have an android library which is distributed as a jar file for inclusion in 3rd party applications.

Within the jar file is an Android Service. The service is exposed through a facade class in the jar file. Hence 3rd parties do not directly bind to the service, they just use the facade class.

I have 2 questions about this architecture:

1) If two completely separate 3rd party applications use this facade (different package names, different user ids etc..) class are two instances of the service created or is a single instance shared?

2) Does it make a difference if the service is bound directly to the 3rd party apps rather than accessed thru a facade?

P.S. http://developer.android.com/reference/android/app/Service.html Does not answer these questions.

+1  A: 

The answer is that only one instance of the service will be running at a time (no matter what, unless you jump through some crazy hoops to make it run multiple times). Multiple calls to Context.startService() and onCreate() do not create additional instances of it. If you are needing to do anything when a 3rd party connects, it should be passed via onStartCommand or the Binder object returned from Context.bindService().

Your model is independent of the underlying Service handling in Android, the behavior would be the same whether your facade was accessing the Service or if 2+ 3rd parties were accessing the Service.

Matt
I know that is definitely the case when a service is used within a single application, however i'm still skeptical that this applies to multiple apps accessing a service provided in a jar file which is re-packaged in each APK. What happens if multiple different versions of the jar are being used?
Declan Shanaghy
From what I've experienced, it will use the currently running service if there is one, and if there isn't it will launch the service that was included in the dex. I really wish the apk had dependency information for situations like this.
Matt
A: 
Declan Shanaghy