Scenario: I have a Controller
(normal Java class) that must be able to pilot several Slave
s.
A Slave
's nature can differ, so that it can either be:
- A
Service
, let's call it aServiceSlave
: the lifecycle of this object usually differs from the application's components (say, it does not depend on the current activity) - A simple Java class, say a
ObjectSlave
: the lifecycle of this object is somewhat bound to the scope in which it is created (say, the current activity)
What these two types of Slave
s have in common, is that they can reside in different processes.
Because of this last "requirement", I immediately turned my attention to AIDL
/Messenger
as the form of communication between a Controller
and a Slave
, since it provides IPC.
However, it seems that AIDL
(and in turn Messenger
, since it should be based on AIDL as well) has only been defined if you are to work with a Service
. That is, there is no way for me to implement an AIDL
-based interface without an IBinder
object, which usually is provided in the onServiceConnected
method.
FIRST QUESTION: can AIDL
really be used only when dealing with a Service
? If it is, why so?
Now, consider my scenario. I would like, as any other good developer would, to write a single, elegant interface that allows the Controller
to pilot every Slave
, regardless of their nature. The only solution that came to my mind so far involves using Intent
s and BroadcastReceiver
s, all conveniently wrapped in dedicated Java classes.
SECOND QUESTION: is this the only viable way? Am I overseeing something?
EDIT
I guess I should've given more details on what the Controller
element actually does. It is a component that is loosely coupled with several UI widgets, who subscribe to it. It has been designed (willingly) so that it doesn't need a reference to a Context
. So it doesn't need or use UI widgets directly, but in turn those widgets depend on the Controller
.