Android-use AIDL for inter-process communication

Source: Internet
Author: User

Although the communication between the client and the service can be implemented through Ibinder, you need to share the business implementation. If the haunted communication between processes needs to be performed using AIDL (Android Interface Definition Language.

AIDL is an interface definition language used to constrain communication rules between two processes. The Compiler generates code to 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 message, the recipient converts the message to a corresponding object. Because the communication information between processes requires bidirectional conversion, android uses the proxy class to implement bidirectional conversion of information. The proxy class is generated by the android compiler and transparent to developers.

The usage is as follows:

1. Define AIDL (similar to the interface but not visible, with the extension. java->. aidl)

// IdownloadService. aidl. Pay attention to the extension.

Package cn. itcast. aidl;

Interface IdownloadService {

Void download (in/out/input String path); // in | out | inout is the direction of the parameter.

}

Ide automatically generates the corresponding java class under the gen package. The interface file generates a stub abstract class, which includes the methods defined by aidl and other auxiliary methods. It is worth noting that asInterface (IBinder iBinder), which returns an interface-type instance. For remote service calls, the object that the remote service returns to the client, the client onServiceConnectionted (ComponentName name, IBinder service) when the method references this object, you cannot directly convert it to an instance of the interface type. Instead, you should use asInterface (IBinder iBinder) for type conversion.

Note the following before writing AIDL:

1. The interface name is the same as the aidl file.

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 default types supported by Aidl include the Basic java types (int, long, boolean, etc.) and (String, List, Map, CharSequence). Using these types, the import declaration is not required, the element types in List and Map must be Aidl-supported types. If a custom type is used as a parameter or return value, the custom type must implement the Parcelable interface.

4. Other Interface Types generated by AIDL of the custom type should be displayed in the aidl description file, even if the type 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 parameter is an input parameter, output parameter, or input/output parameter.

6. The default mark bit in of the original Java type. It cannot be another mark.

Javabean must implement the Parcelable Interface

Class Person implements Parcelable {

Id, name;

Public int describeContents (){

Return 0;

}

// Write data from javabean to Parcel

Public void writeToParcel (Parcel dest, int flags ){

Dest. writeInt (this. id );

Dest. writeString (this. name );

}

// Add a static member named CREATOR, which implements the Parcelable. Creator interface.

Public static final Parcelable. Creator <Person> CREATOR = new Parcelable. Creator <Person> (){

Public Person createFromParcel (Parcel source) {return new Person (source. readInt (), source. readString ());

}

Public Person [] newArray (int size ){

Return new Person [size];

}};

}

Define the aidl declaration file in the custom type package

// Person. aidl. Note that Parcelable is in lower case.

Package cn. itcast. domain;

Parcelable Person;

Interface cn. itcast. domain. Person;

Interface IPersonService {

Void save (in Person person );

}

Create an aidl interface implementation class (implemented by inheriting $ {business interface}. stub class)

Public class ServiceBinder extends IPersonService. Stub {

Public void save (Person person) throws RemoteException {

Log. I ("PersonService", person. getId () + "=" + person. getName ());

}

}

The onBind method that implements the service. The returned value is the aidl implementation class object created in the previous step.

Public IBinder onBind (Intent intent ){

Return new ServiceBinder ();

}

The client uses an implicit intent to access the service.

<Service android: name = ". PersonService">

<Intent-filter>

<Action android: name = "cn. itcast. process. aidl. PersonService"/>

New Intent ("cn. itcast. process. aidl. PersonService ");

Copy the aidl file and the package to the src corresponding to the client. (The client automatically generates the corresponding java class)

This. bindService (, this. SC, BIND_AUTO_CREATE );

SC = new ServiceConnection (){

Public void onServiceConnected (ComponentName, IBinder service ){

PersonService = IPersonService. Stub. asInterface (service );

PersonService. save (new Person (56, "liming "));

}

Public void onServiceDisconnected (ComponetName name ){

PersonService = null;

}

}

Android-End call

Android does not have an API for making public calls. to end a call, you must use AIDL to access the phone Management Service and call the API in the service to end the call. The method is as follows:

1. Copy the following files from the Android source code to the project:

Com/android/internal/telephony/ITelephony. aidl

Android/telephony/NeighboringCellInfo. aidl

As shown in the right figure. The development tool automatically generates ITelephony. java in the gen directory.

2. Call ITelephony. endCall () to end the call:

Method method = Class. forName ("android. OS. ServiceManager ")

. GetMethod ("getService", String. class );

IBinder binder = (IBinder) method. invoke (null, new Object [] {TELEPHONY_SERVICE });

ITelephony telephony = ITelephony. Stub. asInterface (binder );

Telephony. endCall ();

Add permissions to the AndroidManifest. xml file.

<Uses-permission android: name = "android. permission. CALL_PHONE "/
>

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.