Process Communication Using aidl and remote services

Source: Internet
Author: User

In Android, each application has its own process. How can it be implemented when objects need to be transferred between different processes? Obviously, Java does not support cross-Process Memory Sharing. Therefore, to transfer objects, You need to parse the objects into data formats that the operating system can understand to achieve cross-border object access. In Java EE, RMI is used to pass objects through serialization. In Android, aidl (Android Interface Definition Language: Interface Description Language) is used.

Aidl is an interface definition language used to constrain communication rules between two processes for the compiler to generate code and implement inter-process communication (IPC) between two processes on the Android device ). The IPC Mechanism of aidl is similar to the CORBA used by EJB. communication information between processes is first converted to aidl protocol messages and then sent to the other party, after receiving the aidl protocol message, the recipient switches to the corresponding object. Because the communication information between processes requires bidirectional conversion, Android uses the proxy class to implement bidirectional conversion of information behind the scenes. The proxy class is generated by the android compiler and transparent to developers.

Process Communication generally takes the following four steps:

Assume that application a needs to communicate with application B and call the download (string path) method in application B. Application B provides services to application a in service mode. The following four steps are required:

1> Create * in application B *. the definition of the aidl file is similar to that of the interface, for example, in CN. itcast. create idownloadservice under the aidl package. the aidl file contains the following content:
Package CN. itcast. aidl;
Interface idownloadservice {
Void download (string path );
}

After the aidl file is created, eclipse automatically generates the idownloadservice. Java interface file in the project's Gen directory. The interface file generates a stub abstract class, which includes methods defined by aidl and other auxiliary methods. It is worth noting that asinterface (ibinder), which returns an interface-type instance. For remote service calls, the object that the remote service returns to the client is a proxy object, and the client is in onserviceconnected (componentname, when this object is referenced by the ibinder Service) method, it cannot be directly converted to an interface-type instance.
Ibinder) for type conversion.

When writing aidl files, pay attention to the following points:
1. The interface name and aidl file name are the same.
2. You do not need to add access permission modifiers such as public, private, and protected before interfaces and methods, or use final or static.
3. The types of packages supported by aidl by default are Java basic types (INT, long, Boolean, etc.) and (string, list, map, and charsequence). Import Declaration is not required when these types are used. Element Types in list and map must be supported by aidl. If you use a custom type as a parameter or return value, the custom type must implement the parcelable interface.

4. The custom type and other Interface Types generated by aidl should be explicitly imported in the aidl description file, even if the class and the defined package are in the same package.
5. All non-Java basic type parameters in the aidl file must be marked with In, out, And inout to indicate whether the parameters are input parameters, output parameters, or input and output parameters.
6. The default mark of the original Java type is in, and cannot be any other mark.

2> implement the aidl file generation interface (in this example, idownloadservice) in application B, but it is not directly implemented, instead, it is implemented through the stub of the inherited interface (the aidl interface is implemented inside the stub abstract class) and the code that implements the interface method. The content is as follows:
Public class servicebinder extends idownloadservice. Stub {
@ Override
Public void download (string path) throws RemoteException {
Log. I ("downloadservice", PATH );
}
}

3> Create a service in application B, and return the object implementing the aidl INTERFACE IN THE onbind (intent) method of the service (in this example, servicebinder ). The content is as follows:
Public class downloadservice extends Service {
Private servicebinder = new servicebinder ();
@ Override
Public ibinder onbind (intent ){
Return servicebinder;
}

Public class servicebinder extends idownloadservice. Stub {
@ Override
Public void download (string path) throws RemoteException {
Log. I ("downloadservice", PATH );
}
}
}

Other applications can access the service by implicit intent, and the intent actions can be customized. The androidmanifest. xml configuration code is as follows:
<Service android: Name = ". downloadservice">
<Intent-filter>
<Action Android: Name = "cn. itcast. process. aidl. downloadservice"/>
</Intent-filter>
</Service>

4> copy the package of the aidl file in application B together with the aidl file to application a on the client. Eclipse automatically generates the idownloadservice FOR THE aidl file in the gen directory of application. java interface file. Next, you can implement communication with application B in application a. The Code is as follows:
Public class clientactivity extends activity {
Private idownloadservice downloadservice;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
This. bindservice (new intent ("cn. itcast. process. aidl. downloadservice"), this. serviceconnection, bind_auto_create); // bind to the service
}

@ Override
Protected void ondestroy (){
Super. ondestroy ();
This. unbindservice (serviceconnection); // remove the service
}

Private serviceconnection = new serviceconnection (){
@ Override
Public void onserviceconnected (componentname name, ibinder Service ){
Downloadservice = idownloadservice. stub. asinterface (service );
Try {
Downloadservice. Download ("http://www.itcast.cn ");
} Catch (RemoteException e ){
Log. E ("clientactivity", E. tostring ());
}
}

@ Override
Public void onservicedisconnected (componentname name ){
Downloadservice = NULL;
}
};
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.