Android Aidl Summary

Source: Internet
Author: User

/* In Android, each application has its own process, and when it needs to pass objects between different processes, how can it be implemented? Obviously, cross-process memory sharing is not supported in Java. Therefore, to pass objects, the object needs to be parsed into a data format that the operating system can understand to achieve the purpose of cross-premises object access. In Java EE, the use of RMI to pass objects by serialization. In Android, it is implemented using Aidl (Android Interface Definition Language: Interface Description Language).    Aidl is an interface definition language that constrains two inter-process communication rules for the compiler to generate code that implements two interprocess communication (IPC) on Android devices. The IPC mechanism of AIDL and the CORBA used by EJB are very similar, the communication information between processes is first converted into AIDL protocol message, then sent to the other side, the other party receives the AIDL protocol message and then converts it to the corresponding object. Since the communication between the processes requires two-way conversion, Android uses the proxy class behind the two-way transformation of information, the proxy class is generated by the Android compiler, the developer is transparent. Suppose a application needs to communicate with the B application, invoke the download (String path) method in the B application, and the B application provides service to the a application. The following four steps are required: 1> Create the *.aidl file in the b application, the definition of aidl file and the definition of the interface are very similar, such as: Under the Cn.itcast.aidl package to create a idownloadservice.aidl file, the contents are as follows: */
Package cn.itcast.aidl;interface idownloadservice {void Download (String path);

 
public class Servicebinder extends idownloadservice.stub {@Overridepublic void Download (String path) throws remoteexception {log.i ("Downloadservice", Path);}}
3> creates a service (services) in the B application and returns a pair         //image (in this case aidl) that implements the Servicebinder interface in the service's Onbind (Intent Intent) method. The contents are as follows:
public class Downloadservice extends Service {private Servicebinder Servicebinder = new Servicebinder (); @Overridepublic I Binder Onbind (Intent Intent) {return servicebinder;} public class Servicebinder extends idownloadservice.stub {@Overridepublic void Download (String path) throws remoteexception {log.i ("Downloadservice", Path);}}}
Other applications can use implicit intent to access the service, the intent of the action 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 the B application together with the Aidl file to the client a application, Eclipse automatically generates the Idownloadservice.java interface file for the Aidl file in the Gen directory of the A application, Next, you can communicate with the B application in a application, the code is as follows: */
public class Clientactivity extends Activity {private idownloadservice downloadservice; @Overridepublic 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}@ overrideprotected void OnDestroy () {Super.ondestroy () This.unbindservice (serviceconnection);//Decommissioning Service}private Serviceconnection serviceconnection = new Serviceconnection () {@Overridepublic 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 ());}} @Overridepublic 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.