I am creating a .aar dependency for Android which contains a class MyService (packageName: "com.example.MyService") which will run in its own process. I have used AIDL (IMyService) to return binder object using the AIDL interface stub so other applications having the AIDL interface can only access the service. MyService also has an intent-filter with action="com.example.myservice.action".
Let's take a scenario where two apps App1 and App2 with the same dependency class (com.example.MyService) and IMyService.aidl have been installed on the same device.
(1) According to docs, if multiple services have the same intent-filter action, an app binding using that action will bind to randomly any one service. In my case, com.example.MyService is available inside both App1 and App2 (inside their respective packages). So assuming App1 has already started the service, will App2 bind to the same already created MyService or create its own MyService and bind to it? Note that both App1 and App2 will be using the same intent (action: "com.example.myservice.action") and the same AIDL.
(2) I want to ensure that only one instance of the service MyService is running on the device. So if App1 is installed first, it will bind to the service (create it if it is not running) using the above specified action intent. Now, when App2 calls bindservice() using the same intent action, it should bind to the same MyService created by App1. Is this possible?
Intents and your problems will be gone - pskink