Introduction to the use of inter-process communication interfaces based on Android AIDL

Source: Internet
Author: User

AIDL: Android Interface Definition Language, which is a description Language of the android internal process communication Interface. Through this Language, we can define the communication interfaces between processes.

ICP: Interprocess Communication: Internal Process Communication.

Usage:

1. Create an aidl file first. The definition of the aidl file is similar to that of the java code,! It can reference interfaces and classes defined in other aidl files, but cannot reference interfaces and classes defined in custom java class files. to reference custom interfaces or classes, you need to define a corresponding aidl file for this class, and this class must implement the Parcelable interface. At the same time, aidl files and class files must be declared under the same package. Android contains the aidl compiler, when an aidl file is defined, a java file is automatically compiled and saved in the gen directory.

In this project, two aidl files are defined, in which Person implements the Parcelable interface. The following are the definitions of the two aidl files:

Person. aidl

{

Parcelable Person;

}

IAIDLServerService. aidl

{

Package com. webview;
Import com. webview. Person; // reference the above Person. aidl

Interface IAIDLServerService {
String sayHello ();
Person getPerson ();
}

}

2. Compile a Service implementation to define the internal abstract class Stub in the aidl interface. Stub inherits from the Binder and inherits the interface defined in the aidl file. We need to implement these methods. The Stub object is called in the server process, that is, the server process.

Call the aidl interface object defined by the server on the client to implement the Service. onBind (Intent) method. This method returns an IBinder object to the client. When binding a Service, a ServiceConnection object is required. This object is used to receive the IBinder object returned by the Service 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 "Hello AIDL";}@Overridepublic Person getPerson() throws RemoteException {Person person = new Person();person.setName("Livingstone");person.setAge(22);return person;}};}

3. register the Service on the server and add the following code to the Application node!

<Service android: name = "com. webview. AIDLServerService"
Android: process = ": remote">
<Intent-filter>
<Action android: name = "com. webview. IAIDLServerService"> </action>
</Intent-filter>
</Service>

Now, the server process definition has been completed!

4. Compile the client. Note that you need to store a description file of the aidl interface implemented by the server on the client. The client only uses this aidl interface to obtain the aidl object (IAIDLServerService) of the server. stub. after the asInterface (service), you can call the interface-related methods. The object method is called not on the client, but on the server.

Public class MainActivity extends Activity {private Button btn; private IAIDLServerService aidlService = null;
Private ServiceConnection conn = new ServiceConnection () {@ Overridepublic void publish (ComponentName name) {aidlService = null ;}@ Overridepublic void onServiceConnected (ComponentName, IBinder service) {aidlService = IAIDLServerService. stub. asInterface (service); try {aidlService. doFunction (); // Method for executing interface definition} 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.