Why is AIDL / Messenger linked to the Service?

Scenario: I have a Controller

(normal Java class) that needs to be able to pilot multiple Slave

s.

A Slave

nature can be different, so it can be:

  • A Service

    , let's call it ServiceSlave

    : the lifecycle of this object is usually different from application components (let's say it doesn't depend on the current activity)
  • A simple Java class, say ObjectSlave

    : The lifecycle of this object is somewhat related to the scope in which it is created (say the current activity).

What these two types Slave

are is that they can be in different processes .


Because of this last "requirement", I immediately noticed AIDL

/ Messenger

as a form of communication between Controller

and a Slave

, since it provides IPC.

However, it seems that AIDL

(and in turn, Messenger

since it should also be based on AIDL) was only defined if you have to workService

with.That is , there is no way for me to implement an interface AIDL

without an object IBinder

that is usually provided in method onServiceConnected

.

FIRST QUESTION : can it AIDL

really only be used when working with Service

? If so, why?

Now let's look at my scenario. I would like, like any other good developer, to write one elegant interface that allows Controller

everyone to pilot Slave

, regardless of their nature. The only solution that has come down to me so far has to do with using Intent

and BroadcastReceiver

s, they are all conveniently wrapped in dedicated Java classes.

SECOND QUESTION : is this the only viable way? Am I watching something?


EDIT

I guess I should have given more details on what the element actually does Controller

. It is a component that is loosely coupled with several UI widgets that subscribe to it. It was designed (readily) so it doesn't need a link to Context

. Thus, it is not needed or directly uses the UI widgets, but in turn, those widgets depend on Controller

.

+3


source to share


2 answers


This is a good question to answer, but not an easy one. As with most things, there are many approaches to solving this problem. The first thing to look into is whether yours should or should be Controller

using a UI component. If not, then you need to encapsulate it in Service

. Activity

the life cycle is such that it will only work when it is the current thing on the screen. As soon as the user hits HOME or BACK, it is either stopped or destroyed, respectively. Launching another app via a notification or from your app will have an effect similar to pressing HOME: yours Activity

will be paused.

So, if you don't need / want a user interface for yours Controller

, you have a lot of things at your disposal:

  • Make the service a "bound" service by providing an AIDL user interface. Not difficult, but not for a feint of the heart.
  • Make your service a response to a custom Intent

    one to take some action. Service

    can then release broadcasts or start a specific "slave" Activity

    . If you are interested, study usage IntentService

    to better manage streaming and prevent ANR of your application.
  • Like the previous one, use a custom Intent

    and post your subordinate object Messenger

    that it created as a sub in Intent

    . At this moment, Service

    can send messages to Messenger

    , which belongs to the slave Activity

    and will be delivered to a specific one Handler

    .


You don't need a user interface exposed to AIDL for parameters 2 and 3. You are correct in that you must define an AIDL interface if you are going to use a bound service and your method Service.onBind()

must return an instance of your bridging interface insert.

With that said, you can use any of the three approaches to achieve your goal of working with subclasses that are in instances of Activity

or Service

. The advantage of using AIDL approaches or Messenger

is the reduction in context switching (more efficient) since you are not dispatching objects Intent

. Every time you send Intent

, the sender contacts a ActivityManagerService

running system process to allow delivery Intent

. Using AIDL and Messenger

only those calls are bindService()

or startService()

are being addressed ActivityManagerService

. After this point, communication is done using direct binders between the two processes.

+3


source


Off AIDL

and Binder

not attached to Service

s.

While it is true that the reference IBinder

is only provided when binding to Service

, the framework offers a Binder

class that already implements the interface IBinder

.

In addition, the instance Binder

works transparently across processes and does not need to live in context Service

.




[still developing, will provide an outline of how the framework can be implemented by using a Binder instance directly]

+2


source







All Articles