Source code Analysis of custom type transfer in Binder aidl

Source: Internet
Author: User

The binder mechanism implements the IPC and shared memory in different ways, and it takes a Value CopyThe way, the process of passing through the entity follows the Parcelable protocol, the BP side is responsible for writing to the parcel, the BN side is responsible for reading from the parcel restore, the order is agreed by both parties. Prototypes are as follows, the bp/bn end received in fact are just clones of each other.。 Aidl (Android Interface Definition Language) simplifies the use of binders, does a lot of encapsulation but the types that aidl default support include Java primitive types (such as int, long, Boolean, etc.) and the underlying package type ( such as String, List, Map, charsequence), in order to explain how aidl support custom type passing, wrote a demo, as follows: http://files.cnblogs.com/files/tonybright/ Service.zip The following analysis is based on this demo,myactivity needs to start Tonyservice and pass the Setemployee interface, passing an employee object past, and then calling Getemployeeinfo to getEmployee information. 1. The custom type needs to implement the Parcelable interface, override Writetoparcel This API, this will be when the entity is written to parcel using mainactivity call Setemployee code as follows: Mservice.setemployee is a binder call, specifically implemented can be seen Itonyservice.setemployee (BP side) 2. A static variable named creator needs to be defined in the custom type. It implements the Parcelable.creator interface and method, which is used to build an employee instance when reading parcel on the binder bn side. Code implementation in the Itonyservice.setemployee (bn side) This static instance creator is agreed in Aidl, and therefore cannot be replaced by other names. 3. Create a Aidl declaration file with the same name for your custom class employee Employee.aidl, as follows: Package com.example.service;parcelable employee;4. Use the employee's Service aid file Itonyservice.aidl in the following manner:Where in represents the input parameter. 5.Android Studio or IntelliJ has a plug-in that automatically generates parcellable classes: Android parcellable code generator, it's easier. 

Source code Analysis of custom type transfer in Binder aidl

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.