An example of an Android implementation of interprocess communication

Source: Internet
Author: User

The Android service is divided into two types:

Local Service: The same apk is called

Remote service: Called by another apk

The remote service needs to be done with the help of Aidl.

What's aidl?

Aidl (Android Interface Definition Language) is an IDL language used to generate interprocess communication between two processes on an Android device (interprocess communication, IPC) The code. If you are in a process, such as an activity, to invoke an operation on another process (such as a service) object, you can use Aidl to generate serializable parameters.

The AIDL IPC mechanism is interface-oriented, like COM or CORBA, but more lightweight. It is the use of proxy classes to pass data on both the client and the implementation side.

The role of Aidl

Because each application runs in its own process space and can run another service process from the application UI, it often passes objects between different processes. On the Android platform, a process usually does not have access to the memory space of another process, so in order to be able to talk, the object needs to be decomposed into basic units that the operating system understands, and is ordered through the process boundary.

Using code to implement this data transfer process is tedious, and Android provides aidl tools to handle this work.

Choose the occasion of Aidl

The official documentation specifically reminds us when using aidl is necessary: Only you allow clients to access your service from different applications for interprocess communication, and to handle multithreading in your service.

If you do not need concurrent communication (IPC) between different applications, you can should create your interface by implementing a Binder, or you want to do IPC, but do not need to handle multithreading, then implement your interface using a Messenger. In any case, before using Aidl, you must understand how to bind Service--bindservice.

The following example will be given from: http://www.cnblogs.com/lonkiss/archive/2012/10/23/2735548.html

Below is a demo of the use of aidl using an instance of a client-side activity Server service to play music.

Service-side code structure

Client code structure

What is marked is the need to do it.

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.