Android aidl example source code

Source: Internet
Author: User

 

In Android, each application can have its own process. When writing a UI application, services are often used. In different processes, how can we transmit objects? Obviously, cross-Process Memory sharing is not allowed in Java. therefore, objects can only be divided into simple forms that the operating system can understand to achieve cross-border object access. in J2EE, RMI can be used to pass objects through serialization. in Android, aidl is used. in theory, aidl can pass bundle, but it is troublesome to do it.

Aidl (Android Interface Description Language) is an excuse Description Language. the compiler can generate a piece of code through the aidl file and use a pre-defined interface to implement internal communication between two processes. if you want to access an object in another service in one activity, you must first convert the object to a parameter that can be recognized by aidl (possibly multiple parameters ), then, aidl is used to pass these parameters. At the message receiver, these parameters are used to assemble them into desired objects.

The IPC Mechanism of aidl is similar to that of COM or CORBA and is based on interfaces, but it is lightweight. It uses the proxy class to pass values between the client and the Implementation Layer. If you want to use aidl, you need to do two things:

1. Introduce related classes of aidl .;

2. Call the class generated by aidl.

The specific implementation steps are as follows:

1. Create an aidl file and define the interface in this file. This interface defines the methods and attributes that can be accessed by the client. For example, itaskbinder. AdilPackage COM. CMCC. demo; <br/> Import COM. CMCC. demo. itaskcallback; <br/> interface itaskbinder <br/>{< br/> Boolean istaskrunning (); <br/> void stoprunningtask (); <br/> void registercallback (itaskcallback CB); <br/> void unregistercallback (itaskcallback CB); <br/>} 

Here, itaskcallback is defined in the itaskcallback. aidl file:

Package com. CMCC. Demo; <br/> interface itaskcallback <br/>{< br/> void actionreceivmed (INT actionid); <br/>} 

 

Note: theoretically, the parameter can pass the basic data type and string, and the bundle derived class. However, in eclipse, the current ADT does not support bundle as the parameter. It is said that ant compilation is acceptable, I did not try.

2. Compile the aidl file. If ant is used, it may need to be manually. If eclipse plugin is used, java files can be automatically generated and compiled based on the Adil file without human intervention.

3. In the Java file, implement the interface defined in aidl. the compiler will generate a Java Interface Based on the aidl interface. This interface has an internal abstract class named stub, which inherits several methods required to extend the interface and implement remote calls. Next we need to implement several custom interfaces.
Implementation of interfaces in itaskbinder. aidl:

 

 

Private Final itaskbinder. stub mbinder = new itaskbinder. stub () {<br/> Public void stoprunningtask () {// @ todo} <br/> Public Boolean istaskrunning () {// @ todo return false ;} <br/> Public void registercallback (itaskcallback CB) <br/>{< br/> If (CB! = NULL) mcallbacks. Register (CB); <br/>}< br/> Public void unregistercallback (itaskcallback CB) <br/>{< br/> If (CB! = NULL) mcallbacks. unregister (CB); <br/>}< br/>}; <br/> In myactivity. itaskcallback in Java. aidl interface implementation: <br/> private itaskcallback mcallback = new itaskcallback. stub () {<br/> Public void actionreceivmed (int id) {// todo printf ("Callback id =" + id ); <br/>}< br/> }; 

 

4. Provide the itaskbinder interface to the client. if the service is written, extend the service and reload the onbind () method to return an instance of the class that implements the preceding interface. The returned mbinder is the one defined in the embedded above (myservice. Java)
Public ibinder onbind (intent t) {printf ("service on bind"); Return mbinder ;}
In the activity, you can use the interface defined by the binder to remotely call the activity.

5. Callback client functions on the server side. the premise is that when the client obtains the ibinder interface, it needs to register the callback function. Only in this way can the server know the functions called in: myservice. in Java:

Void callback (INT Val) {<br/> final int n = mcallbacks. beginbroadcast (); <br/> for (INT I = 0; I <n; I ++) {<br/> try {<br/> mcallbacks. getbroadcastitem (I ). actionreceivmed (VAL); <br/>} catch (RemoteException E) {<br/> // The remotecallbacklist will take care of removing <br/> // The dead object for us. <br/>}< br/> mcallbacks. finishbroadcast (); <br/>} 

 

 

 

Related Article

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.