Introduction to the use of inter-process communication interface based on Android aidl _android

Source: Internet
Author: User
Tags stub

Aidl:android Interface definition Language, which is a description language of the Android internal process communication interface, through which we can define communication interfaces between processes.

Icp:interprocess communication, internal process communication.

Use:

1, first create a aidl file, aidl file definition and Java code similar, but! It can refer to interfaces and classes defined in other Aidl files, but cannot refer to interfaces and classes defined in a custom Java class file, to refer to a custom interface or class, and to define a corresponding Aidl file for this class, and this class implements the Parcelable interface. At the same time, aidl files and class files must be declared under the same package; Android contains the Aidl compiler, which, when defined as a Aidl file, automatically compiles and generates a Java file, which is saved under the Gen directory.

In this project, two aidl files are defined, where the person implements the interface parcelable, and the following is the definition of the two aidl files:

Person.aidl

{

Parcelable person;

}

Iaidlserverservice.aidl

{

Package Com.webview;
Import com.webview.person;//refer to the person.aidl above

Interface iaidlserverservice{
String SayHello ();
Person Getperson ();
}

}

2. Write a service implementation definition AIDL the internal abstract class in the interface stub,stub inherit from binder and inherit the interfaces we defined in Aidl file, we need to implement these methods. Stub object is called in the service-side process, that is, the server-side process.

The client invokes the Aidl interface object defined by the server and implements the Service.onbind (Intent) method, which returns a IBinder object to the client, which requires a Serviceconnection object when binding the service. This object is actually the IBinder object that is used to receive service returns when the client binds the service.

|| public static abstract class Stub extends Android.os.Binder implements Com.webview.IAIDLServerService

public class Aidlserverservice extends service{@Overridepublic ibinder onbind (Intent Intent) {return binder;} Private Iaidlserverservice.stub binder = new Stub () {@Overridepublic String SayHello () throws RemoteException {return "Hel Lo aidl ";} @Overridepublic person Getperson () throws remoteexception {person person = new person ();p erson.setname ("Livingstone"); Person.setage; return to Person;}}

3, in the service end of the registration service, add the following code into the application node!

<service android:name= "Com.webview.AIDLServerService"
android:process= ": Remote" >
<intent-filter>
<action android:name= "Com.webview.IAIDLServerService" ></action>
</intent-filter>
</service>

At this point, the server-side process definition is complete!

4, write clients, pay attention to the need to save a server on the client side of the implementation of the Aidl interface description file, the client just use the Aidl interface, get the server Aidl object (IAIDLServerService.Stub.asInterface (service)) The associated methods of the interface can then be invoked, and the object's method invocation is performed on the server rather than on the client.

public class Mainactivity extends activity {private Button btn;private iaidlserverservice aidlservice = null;
Private Serviceconnection conn = new Serviceconnection () {@Overridepublic void onservicedisconnected (componentname name ) {aidlservice = null;} @Overridepublic void onserviceconnected (componentname name, IBinder service) {Aidlservice = IAIDLServerService.Stub.asInterface (service); try {aidlservice.dofunction ();//execute interface definition related methods} catch (RemoteException E {E.printstacktrace ();}}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); btn = (Button) Findviewbyid (R.id.button); TV = (TextView) Findviewbyid (R.id.textview); Btn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent service = new Intent (" Com.webview.IAIDLServerService "); Bindservice (service, Conn, bind_auto_create);/Bind Service}});}

Client directory structure:

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.