Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 955
Coldfish Avatar asked Dec 27 '15 19:12

Coldfish


2 Answers

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.

like image 99
rupesh jain Avatar answered Nov 18 '22 02:11

rupesh jain


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).

like image 43
David Wasser Avatar answered Nov 18 '22 01:11

David Wasser