Use AIDL to bind a Service

Source: Internet
Author: User

AIDL is called the Android Interface Definition Language. It enables your program to implement inter-process communication (IPC), and allows multi-thread access based on the implementation of IPC.

First, create your own. aidl file (see IRemoteService. aidl In Demo ).

Defining the AIDL file is very similar to creating an interface in java. Generally, aidl supports five data types: java basic data types; String; CharSequence; List; Map. List and Map are special (http://developer.android.com/guide/components/aidl.html#Create ). If the data type you use is not the basic data type of AIDL, you must use the import Statement to import it, even if they are in the same package. When declaring a method, the method parameters can be zero to multiple, And the return value can be void. For all non-basic data types, you must specify whether to pass in or out the value (the basic data type is the input value; static fields cannot be declared in the AIDL file)

Secondly, we need to implement the interface we just created (see the mBinder in StudentService in the Demo ).

Suppose we have an AIDL file named IRemoteService. aidl. When we compile our project, android ant can generate our AIDL file into a java file (put it under gen ). In this java file, an abstract internal class Stub (inheriting the Binder class) implements our interface and provides an asInterface method to convert the IBinder object to our interface type. Because the implementation of our interface is converted to implementing its abstract internal class Stub (Service-side business functions are all implemented here ).

Once again, expose our interface to the client (note that the Demo declares StudentService in AndroidManifest. xml on the Server)

Exposing our interface to the client is actually: reverse our Stub class instance in the onBind function of the Service.

In this way, the Service end is finished!

So, how to pass objects through IPC (see Student. java and Student. aidl in the Demo )?

If you want to pass objects between processes, the object must implement the Parcelable interface. To implement this interface, we need to complete the following steps:

1. Implement the Parcelable interface when declaring an object

2. Implement the writeToPacel Method

3. Add a static variable named CREATOR (this variable must implement the Parcelable. Creator interface)

4. Create an AIDL file to declare the class that implements the Parcelable interface.

In this way, we can use objects in AIDL.

How does the client call the Service method?

In fact, it is very simple, just a few steps (see the code in the Client in the Demo ).

1. Use. copy the aidl file to the client. If your aidl file only describes the classes that implement the Parcelable interface, copy the corresponding java file. (Note the package name. For details, refer to the import of the AIDL file on the server client of the Demo)

2. Compile the client program and generate the class file corresponding to aidl in the gen/directory.

3. Implement the ServiceConnection interface. In the onServiceConnected method, use the Java file function generated by AIDL (the asInterface of the internal class Stub) to convert the IBinder parameter in the onServiceConnected function to the interface object generated by AIDL.

4. Call the functions in AIDL Based on the object obtained in step 1.

5. Call the bindService function to perform the binding operation (unbindService function is used for unbinding)

Here, the usage of AIDL is over. We recommend that you read this article using the Demo of this article. I believe this will benefit you a lot.

Demo link: http://pan.baidu.com/share/link? Consumer id = 587849 & uk = 2953765628

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.