Binder-based interprocess communication in Android

Source: Internet
Author: User
Tags app service

Abstract: The working mechanism of Binder is analyzed . This paper first briefly describes the comparison between the binder mechanism in Android and the traditional Linux process , and then the process analysis based on the binder interprocess communication. Combine development examples (AIDL)to learn about Binder-based interprocess communication .

android uses Binder as the IPC (Internet Process Connection) Advantages of the mechanism

The IPC means of interprocess communication already owned by Linux include (Internet process Connection): pipelines, signals and traces, sockets, message queues, shared memory, and semaphores. However, there are problems in the way of communication between Linux processes, such as increasing process overhead, process overloading and security vulnerabilities. in the Android system, theBinder IPC interprocess communication mechanism can effectively solve these problems .

Advantages of Binder:

① High efficiency: Binder transfer only need one copy, and pipeline, message queue, socket need to copy two times, for mobile devices, performance has always been a big problem;

② Security High: Binder mechanism for the identity of both sides of the communication is the kernel for school-check support, socket mode only need to know the address can be connected;

③ Ease of use : b/s mode of communication, if the pipeline/message queue has to be packaged, and binder is designed in an object-oriented manner, making a remote procedure call is like invoking a local object;

2, based on binder process analysis of interprocess communication

   binder IPC with running results , This is similar to a " lightweight " The remote invocation process.

binder mechanism is through server/client architecture implemented, mainly including server,client,service Manager and Binder driver. Where Server,client,service Manager runs in user space, the drive runs in kernel space. (such as)

3, combined with development examples (AIDL)

3.1 Case Requirements hypothesis

An app service provides a title to the information of the remote services, as a service side, another application in the Activity wants to get a book title information. Android data is delivered in a aidl way.

3.2 Case Study
Different apps for Android can't share data, so you can only use interprocess communication to complete app-shared data if needed.

interprocess communication (IPC) involves three parts: the client (caller), the delivery data, the service (callee). Where service-side services are called by the same application and different application invocation principles are different.

It is therefore divided into the following two types:

① Local Service: service is called by Activity in the same application to implement some time-consuming tasks for the application itself.

② remote Sercie: Can be reused by other applications, such as WiFi service, other applications do not need to write such services, call the existing can.

This case selects local service.

3.3 Aidl Case Implementation principle

From the Android application layer, Binder is the medium of communication between the client and the server, and when you bindservice, the server returns a Binder object that contains the service-side business call.

With this binder object, the client can obtain the service or data provided by the server. such as

3.4 Aidl Case Implementation steps

① Create a BookInfo class to inherit the service

② Create a Ibookinfo interface, write a method for the corresponding aidl file

Private String GetNames () {        log.e ("jianchenglovebiancheng""  Myservice.getname");         return " "The Ordinary World" " ;    }

③ registering services in the feature manifest file

<service android:name="com.example.aidl.BookInfo" >            <intent-filter>                <action android:name="com. JCLoveBiancheng.binder.action.AIDLService" />            </intent-filter>        </service>

④ the classes generated by the Aidl file, bind in the service

Private Final New ibookinfo.stub () {        @Override        publicthrows  remoteexception {            LOG.E ( "Jianchenglovebiancheng", "MyService.AIDLService.getName");             return GetNames ();        }    }; If you can't find Ibookinfo, rebuild project can

⑤ bind mode to start service

  PrivateIbookinfo Mservice;//proxy classes for services    PrivateServiceconnection mconnection =Newserviceconnection () { Public voidonserviceconnected (componentname className, IBinder service) {LOG.E ("Jclovebiancheng", "mainactivity.onserviceconnected"); Mservice=IBookInfo.Stub.asInterface (service); }         Public voidonservicedisconnected (componentname className) {LOG.E ("Jclovebiancheng", "mainactivity.onservicedisconnected"); Mservice=NULL; }    };

⑦ Open Service

New Intent (mainactivity.  this, BookInfo. class );            Intent.setaction ("com. JCLoveBiancheng.binder.action.AIDLService ");            Bindservice (Intent, mconnection, context.bind_auto_create);

⑧ Click the button to get information from the service (e.g.)

Click Bind Service to print out:

Click the Unbind service to print out:

Click Get Info to print out:

Additional:

Data types supported by Aidl
Basic data types: int, long, char, Boolean, double, String, and Charsequence
List:
Only support ArrayList, each element inside must be able to be aidl supported
MAP:
Only support HashMap, each element inside must be able to be aidl supported
Parcelable:
All objects that implement the Parcelable interface
Aidl:
All Aidl interfaces themselves can also be used in the Aidl file.

Demo:http://files.cnblogs.com/files/wujiancheng/aidl%e5%b0%8f%e6%a1%88%e4%be%8b.zip

Binder-based interprocess communication in Android

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.