How to run a singleton (shared) service in a library for multiple applications?
Asked Answered
F

2

14

I've written a library starting a service in the background. It runs perfectly in all applications.

In order to reduce the RAM usage, I want to avoid running multiple services for different applications. Actually, it's pretty enough to use only one service to get things done.

Firstly, I've written an AIDL file to make IPC between applications/libraries. Defined the service as exported/enabled with signature permission. Since all applications are the exactly the same service, it's not possible to check if any one is up or down. While binding the service to check the condition of the service, it always creates and destroys the own service because of the nature of BIND_AUTO_CREATE flag. That's why not possible to get any kind of info from the exported service if it's really up and running.

Then, I tried to define a Content Provider to the manifest of the library. My aim is to share the service info through it. It's really good mechanism to communicate between exported service and application main process. But it is not usable for multiple instances. Because applications which gets the content provider info from the library use the same authority and so it's not possible to install the second one. It gives an DUPLICATE_PROVIDER_AUTHORITY error.

What's your suggestion about the issue? Is there any option to create a master/slave mechanism? Is it possible to make the service singleton for the application uses the library project?

P.S: Tried broadcast and shared preferences techniques. But they're not effective to listen the callback from the exported service.

Ferromagnetic answered 27/12, 2015 at 19:7 Comment(1)
would you be able to share the solution you came up with please? Even if it was not ideal to what you wanted.... You said in a comment 'I completed my implementation with broadcast receivers (using signature permission)'. It would be interesting to see how you handled this.Careycarfare
J
6

You need to put the Service in an APK of its own. It needs to have its own unique package name (in the manifest) which is different from the package names of any of the applications that use it. This is how you make the Service behave as a singleton. Now you can use AIDL and bind to the Service in order to have two-way communication.

Note that in more recent versions of Android, it has become necessary to start a Service using an explicit Intent (ie: the Component must be explicitly specified, you can't use just an ACTION).

Joyner answered 27/12, 2015 at 20:35 Comment(3)
Thanks for the explanation. But I am just the author of the library, not application. My library provides an unique solution to the customer. It works well on the production right now. From my side, there's no way to know application names, details, etc. Every customer can use it in their applications. I need a solution on the library side.Ferromagnetic
Actually, I have a life-check mechanism between services in my libraries via broadcast system. But I want to write more efficient code.Ferromagnetic
There's nothing you can do, then. You'll have to live with multiple Services, since there is no way that they can communicate or know about each other. You would eventually end up with a permissions/security nightmare anyway. Sorry.Joyner
M
1

Alternative 1:

  • If the use case permits I think you should not implement the Service. Make your client implement a service a call your library code. This is how MediaPlayer and other default android APIs work.

Alternative 2:

  • Host the service in a separate app..and download the app when the first call is made from any client. From here onwards there will be single service handling all the client request.This is how some APIs like adobe air/ MDM solutions from Airwatch works.

There is no good way you can control a component which is running in other app,unless using broadcast receivers and all.

Marvismarwin answered 4/1, 2016 at 21:11 Comment(2)
I completed my implementation with broadcast receivers (using signature permission). Thanks for your alternative solutions.Ferromagnetic
@StillHopeForMe can you elaborate on your solution ?Oraleeoralia

© 2022 - 2024 — McMap. All rights reserved.