Android Interface Definition Language-aidl (1)

Source: Internet
Author: User

This article translated from: http://developer.android.com/guide/components/aidl.html


Aidl (androidinterface definitionlanguage) is similar to other idls used by you. To enable inter-process communication (IPC), it allows you to define programming interfaces recognized by both the client and the server. On Android, a process generally cannot access the memory of another process. Therefore, they need to be decomposed into the original objects that the operating system can understand and compiled into objects that span boundaries. Compiling grouping code is very troublesome, so android will use aidl to process it.

Note: aidl is necessary only when clients of different applications are allowed to access the server through IPC and want to process multithreading on the server. If you do not need to execute concurrent IPC processing across different applications, you should create an interface by implementing a binder. Alternatively, if you want to execute IPC processing, but do not need to process multiple threads, you can use the messenger class to implement the interface. Therefore, before implementing aidl, you must understand Android boundservices.

Before designing the aidl interface, you must note that the aidl interface is called directly. You should not assume the thread in which the related call will occur. In particular, the processing varies depending on whether the call is from a local process thread or a remote process thread:

1. Calls from local processes will be executed in the same thread as the caller. If this is the main UI thread, the thread will continue to be executed in the aidl interface. If it is another thread, the call routine is one of the code to be executed in the service. Therefore, only the local thread that is accessing the service can fully control the thread in which the call will be executed (however, in this case, aidl is not required at all, instead, you should create an interface by implementing the binder ).

2. Calls from remote processes will be allocated to the internal thread pool of processes maintained by the platform. You must be prepared for unknown threads that occur multiple times at the same time. In other words, the implementation of the aidl interface must be thread-safe.

3. The key times of oneway are used to modify the remote call behavior. When this keyword is used, the remote call is not blocked. It only sends the transaction data and returns immediately. The implementation of the interface is to receive Common Remote calls according to the calling rules of the binder thread pool. If oneway is used in a local call, there will be no impact and the call is still asynchronous.

DefinitionAidlInterface

The aidl interface must be defined using the Java programming language syntax, save it in the source code of the application that holds the service and the application that binds the service (in the src/directory ).

When creating an application that contains the. aidl file, the androidsdk generates an ibinder Interface Based on the. aidl file and stores the interface file in the GEN/directory. The service must implement the ibinder interface correctly. Then the client application can bind the service and call the methods in ibinder to execute inter-process communication (IPC ).

Follow these steps to create a binding service using aidl:

1. Create a. aidl File

This file defines the programming interface with method signature.

2. Implement the interface defined in the. aidl File

The androidsdk tool is based on the. aidl file and uses the Java programming language to generate this interface. This interface has an internal abstract class named stub, which inherits the binder class and implements the methods defined in the aidl interface. You must inherit stub class and implement these methods.

3. Expose the interface to the client

Implement a service and override the onbind () method to return the implementation of the stub class.

Warning any changes to the API after the first release of the aidl API must maintain backward compatibility to avoid interruptions to other applications using the service. To allow other applications to access the service interface. the aidl file is copied to the application that uses the service. Therefore, you must maintain support for the initially defined interface.

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.