Android Aidl for interprocess communication (IPC)

Source: Internet
Author: User

When writing aidl files, you need to be aware of:

1. The interface name and the Aidl file name are the same.

2. Interfaces and methods do not use the access modifier (public, private, protected, etc., also can not be used final, static).

The 3.AIDL default supported types include Java primitives (int, long, Boolean, and so on) and (String, List, Map, charsequence) that do not require an import declaration when using these types. The element types in list and map must be aidl supported types, and if custom types are used as parameters or return values, the custom type must implement the Parcelable interface.

4. Custom types and other interface types generated by Aidl, in the Aidl profile, should be explicitly import, even if the class and the defined package are in the same package.

5. All non-Java primitive type parameters in the Aidl file must be prefixed with an in, out, InOut flag to indicate whether the parameter is an input parameter, an output parameter, or an input/output parameter.

6.Java The original type is marked in by default and cannot be a different token.

Studentquery-aidl file: (common to both parties)

// because it is an interface description, it is similar to the interface, Aidl Interface studentquery{    String querystudent (int//  query student name by ID }

Remote server: (implicitly adding action:com.sample.student.query in the manifest file for remote services)

1  Public  classStudentqueryserviceextendsService {2 3       PrivateString[] names = {"Zhang San", "John Doe", "Zhao Wu"};4       PrivateIBinder Binder =NewStudentquerybinder ();5  6 @Overrider7        Publicibinder onbind (Intent Intent) {8             returnBinder;9       }Ten  One       Private Final classStudentquerybinderextendsStudentquery.stub { A  -                  PublicString Querystudent (intNumberthrowsRemoteException { -                          returnquery (number); the                 } -       } -  -       PrivateString Query (intNumber ) { +  -               if(Number > 0 && number < 4) { +                       returnNames[number-1]; A               } at               return"No student number"; -       } -}

Client (Activity):

1  Public classMainactivityextendsActivity {2 3        Privatestudentquery studentquery;4        PrivateStudentconnection conn =Newstudentconnection ();5        6 @Override7         Public voidonCreate (Bundle savedinstancestate) {8             Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.main);Ten              OneIntent Service =NewIntent ("Com.sample.student.query"); A Bindservice (Service, Conn, bind_auto_create); -        } -  the        //Click Query -         Public voidquerystudent (view view) {
try { -Studentquery.querystudent (0);
} catch (RemoteException e) {
E.printstacktrace ();
} - } + - Private Final classStudentconnectionImplementsserviceconnection { + A Public voidonserviceconnected (componentname name, IBinder service) { at //the Asinterface conversion proxy object is an interface type. -Studentquery =StudentQuery.Stub.asInterface (service); - } - - Public voidonservicedisconnected (componentname name) { -Studentquery =NULL; in } - } to + @Override - protected voidOnDestroy () { the //destroyed * Unbindservice (conn); $ Super. OnDestroy ();Panax Notoginseng } -}

Pass custom type parameters between processes:

1) The custom type must implement the Parcelable interface and implement the Writetoparcel method of the Parcelable interface:

1  Public void int flags)

2) The custom type must contain a static member named Creator, which requires the implementation of the Parcelable.createor interface and its methods.

3) Create a aidl file to declare your custom type.

The role of the Parcelable interface:

An instance that implements the Parcelable interface can write its own state information (which is usually referred to as the value of each member variable) to parcel, or it can restore its state from parcel and parcel to complete the serialization of the data.

Aidl Mode of communication:

Cross-process memory sharing is not supported in Java. In Java EE, the use of RMI to pass objects by serialization.

Aidl is an interface definition language that constrains two interprocess communication rules for compiler code generation, implements two interprocess communication (IPC) on Android devices, Aidl's IPC mechanism and the CORBA used by EJBS are very similar, inter-process communication information, First will be converted to AIDL protocol message, and then sent to the other side, the other side received the AIDL protocol message and then converted to the response of the object, because the communication between the process of information informatics needs two-way conversion, so the Android using proxy class behind the implementation of the two-way conversion, proxy class by the Android compiler generated.

Android Aidl for interprocess communication (IPC)

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.