Android Parcel Usage Detailed _android

Source: Internet
Author: User
Tags int size

This example describes the parcel usage in Android. Share to everyone for your reference, specific as follows:

The use of parcel in Android, a container for storing basic data types and reference data types, binds data between processes through IBinder in Andorid.

Parcel Parcel = Parcel.obtain ();//Get a Parcel object

Here's how it can be done, createxxx (), wirtexxx (), readxxx (),
where Dataposition () returns the current parcel offset of the current object's stored data, and setdataposition () sets the offset of the current parcel object to facilitate reading of the data in the parcel. The problem is that the data that I read is either empty (null) or always the first offset to store and read the data. Parcel what mechanism to implement, is stored in what form, then I can arbitrarily to its operation, read the target data.

Value range of base 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 as the base unit stores written variables, 4byte*8=32bit, in memory the reference address variable is encoded in 16, and as an offset, that is, the offset is a multiple 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 the natural number}

I think there's absolutely no way to offset is 3,6,9 data like this ...

From this we can infer that, whether he stores a basic data type or a variable that refers to a data type, it is offset by the 32bit base 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 you write data, in 32bit space to store the variables to put in, how to occupy an offset, also one of the 4 positions, and when stored data such as parcel.writefloat ( 1000000000000000000000000000000000000F); He will automatically move backwards,

Parcel.writestring ("a");
Parcel.writestring ("B");
Parcel.writestring ("D");
Parcel.writestring ("C");

And

Parcel.writestring ("ABCD");

The difference. This shows that the allocation of his memory is the original.

So how can I get the books I put in in order to get out of it? Setdataposition (), set parcel offset, in readxxx (), read 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 + +;
}

This shows that parcel write data is in accordance with the 32bit as the basic container, sequentially store the written data, basic and reference (in fact, the reference is also a combination of several basic data types into objects-Properties | method), When we read it, we can take the data that we've already stored, based on the offset position (curr_position) of the target data, and the size of the offset.

int i = curr_position; while
(i <= size) {
parcel.setdataposition (i);
int curr_int = PARCEL.READXXXT ();
i+=4;
int j = 0;
j + +;
}

That's OK.

His createxxx () method is useless now, use to say it!

To sum up, the range of basic data types in Java, the reference type of data, the equivalent of pointers in C, and the reciprocal conversion and flexible references between the various systems, and customizing the arbitrary data types that you want.

For more information on Android-related content readers can view the site topics: "Android Database Operating skills summary", "Android programming activity Operation Skills Summary", "Android File Operation skills Summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.