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 DefinitionLanguage: 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. Generally, the following four steps are required to implement process Communication: Assume that application A needs to communicate with application B and call the download (Stringpath) method in application B, application B provides services to application A as A Service. 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: [java] packagecn. itcast. aidl; interface IDownloadService {void download (String path);} after the aidl file is created, eclipse automatically generates the IDownloadService synchronously in the project's gen directory. java interface file. The interface file generates a Stub abstract class, which includes methods defined by aidl and other auxiliary methods. It is worth noting that asInterface (IBinderiBinder) 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 (ComponentNamename, IBinderservice) when the method references this object, you cannot directly convert it to an instance of the interface type, but you should use asInterface (IBinderiBinder) for type conversion. When writing Aidl files, note the following: 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: [java] 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, onBind (Intentintent) of the Service) (In this example, ServiceBinder ). The content is as follows: [java] public class DownloadService extends Service {private ServiceBinder serviceBinder = new ServiceBinder (); @ Override public IBinder onBind (Intent 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. The intent actions can be customized. AndroidManifest. the xml configuration code is as follows: <serviceandroid: 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. the java interface file can be used to communicate with application B in application A. The Code is as follows: [java] 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 service} @ Override protected void onDestroy () {super. onDestroy (); this. unbindService (serviceConnection); // remove service} private ServiceConnection serviceConnection = new ServiceConnection () {@ Override public void onServiceConnected (ComponentName, IBinder service) {downloadService = IDownloadService. stub. asInterface (service); try {www.2cto.com downloadService. download ("http://www.itcast.cn");} catch (RemoteException e) {Log. e ("ClientActivity", e. toString () ;}@ Override public void onServiceDisconnected (ComponentName name) {downloadService = null ;}};}