Android Development: What Is parcel (2)

Source: Internet
Author: User

The previous response explains ibinder. This is a detailed explanation of parcel. The following is an explanation of the android SDK documentation:
Parcel is a container mainly used to store serialized data, which can then be passed between processes through the binder (to understand why serialization is required, see: http://blog.csdn.net/nkmnkm/archive/2011/05/28/6451699.aspx ). Parcel can contain the original data types (written using various corresponding methods, such as writeint () and writefloat (), and can contain parcelable objects, it also contains a reference to an active ibinder object, which causes the other end to receive a proxy ibinder pointing to this ibinder.

Note: parcel is not a serialization mechanism for general purposes. This class is designed for high-performance IPC transmission. Therefore, it is not suitable to write parcel into permanent storage because changes to the data type in parcel will make the old data unreadable.

The APIs of parcel are used to read and write different types of data. These functions are mainly of six types.

1. Original class

These methods mainly read and write raw data types. They are: writebyte (byte), readbyte (), writedouble (double), readdouble (), writefloat (float), readfloat (), writeint (INT), readint (), writelong (long), readlong (), writestring (string), readstring (). most other data operations are based on these methods.

2. Original array class

These methods are used to read and write arrays composed of raw data. Write the length of the array before writing data to the array. The read array method can read data to an existing array, or create and return a new array. They are:

  • Writebooleanarray (Boolean []), readbooleanarray (Boolean []), createbooleanarray ()
    Writebytearray (byte []), writebytearray (byte [], Int, INT), readbytearray (byte []), createbytearray ()
    Writechararray (char []), readchararray (char []), createchararray ()
    Writedoublearray (double []), readdoublearray (double []), createdoublearray ()
    Writefloatarray (float []), readfloatarray (float []), createfloatarray ()
    Writeintarray (INT []), readintarray (INT []), createintarray ()
    Writelongarray (long []), readlongarray (long []), createlongarray ()
    Writestringarray (string []), readstringarray (string []), createstringarray ().
    Writesparsebooleanarray (sparsebooleanarray), readsparsebooleanarray ().

3 parcelable class
Parcelable provides an extremely efficient protocol for objects to read and write data from parcel. You can use the direct method writeparcelable (parcelable, INT) and readparcelable (classloader) or writeparcelablearray (T [], INT) and readparcelablearray (classloader) for read and write. These methods write the information and data of the class to parcel, so that the appropriate class loader can be used to reconstruct the class instance in the future.

Some other methods provide more efficient ways to operate parcelable: writetypedarray (T [], INT), writetypedlist (list), readtypedarray (T [], parcelable. creator) and readtypedlist (list, parcelable. creator ). These methods do not write class information. Instead, they must be able to read the data and pass in the correct parcelable. creator to create an object instead of directly constructing a new object. (A more efficient way to read and write a single parcelable object is to directly call parcelable. writetoparcel () and parcelable. creator. createfromparcel ())

4 bundles class

Bundles is a type of secure map container that can be used to store any different types of data. It has a lot of performance optimizations for writable data, and its type security mechanism avoids debugging of bugs caused by type errors when data is sent to parcel, the following methods can be used: writebundle (bundle), readbundle (), and readbundle (classloader ).

5. Activity object class

An extraordinary feature of parcel is the ability to read and write live objects. For activity objects, their content is not actually written, but only a token is written to reference this object. When reading this object from parcel, you do not get a new object instance, but directly get the written object. There are two types of activity objects available:

Binder object. It is the basis for Android cross-process communication. This type of object can be written to parcel, and you will get the original object or a proxy object (you can imagine that you get the original object in the process, obtain the proxy object during the process ). Available Methods: writestrongbinder (ibinder), writestronginterface (iinterface), readstrongbinder (), writebinderarray (ibinder []), readbinderarray (ibinder []), createbinderarray (), writebinderlist (list), readbinderlist (list), createbinderarraylist ().

Filedescriptor object. It represents the original Linux file descriptor. It can be written to parcel and a parcelfiledescriptor object will be returned during reading to operate on the original file descriptor. Parcelfiledescriptor is a copy of the original descriptor: the object is different from FD, but they all operate on the same file stream and use the same file location pointer. You can use writefiledescriptor (filedescriptor) and readfiledescriptor ().

6 NO-type container class

A Class of final methods for reading and writing standard Java container classes. These methods are: writearray (object []), readarray (classloader), writelist (list), readlist (list, classloader), readarraylist (classloader), writemap (MAP ), readmap (MAP, classloader), writesparsearray (sparsearray), readsparsearray (classloader ).

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.