Detailed Explanation of Usage of Parcel in Android

  • 2021-07-26 08:54:23
  • OfStack

This article illustrates the usage of Parcel in Android. Share it for your reference, as follows:

Android in the use of Parcel, he is a storage of basic data types and reference data types of the container, in andorid through IBinder to bind data in the inter-process transfer of data.


Parcel parcel = Parcel.obtain();//  Get 1 A Parcel  Object 

The following methods can be used, createXXX (), wirteXXX (), readXXX (),
Among them, dataPosition () returns the offset of data stored in the current Parcel object, while setDataPosition () sets the offset of the current Parcel object to facilitate reading the data in parcel, but the problem lies in that the data I read is either empty (null) or always the value at the first offset to store and read the data. Parcel uses what mechanism to achieve, is in what form of storage, and then I can arbitrarily to its operation, read the target data.

Value range of basic data type:

boolean 1bit
short 16bit
int 32bit
long 64bit
float 32bit
double 64bit
char 16bit
byte 8bit

From this, I can guess that Parcel 32bit stores the written variables as the basic unit, 4byte*8=32bit, and the reference address variables in memory are encoded in hexadecimal and are used as offsets, that is, offsets are multiples of 4, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48...... 4*N,

f (x) = 4*y {y > =0 & y is a natural number}

I don't think there will be any data with offsets of 3, 6 and 9. . .

It can be inferred from this that whether it stores variables of basic data type or reference data type, it is offset by 32bit basic unit.


parcel.writeInt(1);
parcel.writeInt(2);
parcel.writeInt(3);
parcel.writeInt(4);
parcel.writeInt(5);
parcel.writeInt(6);
parcel.writeInt(7);
parcel.writeInt(81011111);
parcel.writeFloat(1f);
parcel.writeFloat(1000000000000000000000000000000000000f);

parcel. writeXXX (), every time the data is written, the variables to be put can be stored in the space of 32bit, so it only occupies one offset, and it moves four positions, while when the stored data is such as parcel. writeFloat ( He moves back automatically,


parcel.writeString("a");
parcel.writeString("b");
parcel.writeString("d");
parcel.writeString("c");

And


parcel.writeString("abcd");

The difference. It can be seen that his memory allocation turned out to be like this.

Then how can I get the books I saved out in turn? setDataPosition (), set the offset of parcel, and in readXXX (), read the data


int size = parcel.dataSize();
int i = 0;
while (i <= size ) {
parcel.setDataPosition(i);
int curr_int = parcel.readInt();
i+=4;
int j = 0;
j++;
}

It can be seen that parcel writes data according to 32bit as the basic container, The written data, basic data and reference are stored in turn (in fact, the reference is also OBJECTS-attribute | method composed of multiple basic data types). When reading, we can take out the stored data according to the position of the offset of the target data (curr_position) and the size of the offset (size)


int i  =  curr_position ; 
while (i <= size ) {
parcel.setDataPosition(i);
int curr_int = parcel.readXXXt();
i+=4;
int j = 0;
j++;
}

This is ok

His createXXX () method is useless now, so let's talk about it!

To sum up, the value range of basic data types in Java, the data of reference types, which are equivalent to pointers in c, the mutual conversion and flexible reference between various binary systems, and the customization of any binary data types you want.

For more readers interested in Android related content, please check the topics on this site: "Summary of Android database operation skills", "Summary of activity operation skills for Android programming", "Summary of SD file operation skills", "Summary of SD card operation methods for Android programming development", "Introduction and Advanced Tutorial for Android development", "Summary of Android resource operation skills", "Summary of Android view View control usage"

I hope this article is helpful to everyone's Android programming.


Related articles: