parcel和parcelable

简介: <p style="color:rgb(51,51,51); font-family:Arial; font-size:13.63636302948px; line-height:26px"> Parcel 在英文中有两个意思,其一是名词,为包裹,小包的意思; 其二为动词,意为打包,扎包。邮寄快递中的包裹也用的是这个词。Android采用这个词来表示封装消息数据。这个是通过IBinder

Parcel 在英文中有两个意思,其一是名词,为包裹,小包的意思; 其二为动词,意为打包,扎包。邮寄快递中的包裹也用的是这个词。Android采用这个词来表示封装消息数据。这个是通过IBinder通信的消息的载体。需要明确的是Parcel用来存放数据的是内存(RAM),而不是永久性介质(Nand等)。

Parcelable,定义了将数据写入Parcel,和从Parcel中读出的接口。一个实体(用类来表示),如果需要封装到消息中去,就必须实现这一接口,实现了这一接口,该实体就成为“可打包的”了。

接口的定义如下:

[java]  view plain copy
  1. public interface Parcelable {  
  2.     //内容描述接口,基本不用管  
  3.     public int describeContents();  
  4.     //写入接口函数,打包  
  5.     public void writeToParcel(Parcel dest, int flags);  
  6.      //读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入。  
  7.     //为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例。  
  8.     public interface Creator<T> {  
  9.            public T createFromParcel(Parcel source);  
  10.            public T[] newArray(int size);  
  11.        }  

在实现Parcelable的实现中,规定了必须定义一个静态成员, 初始化为嵌入接口的实现类。

[java]  view plain copy
  1. public static Parcel.Creator<DrievedClassName>  CREATOR =  
  2.     new Parcel.Creator<DrievedClassName>();   

下面定义了一个简单类MyMessage, 他需要把自身的数据mdata,打入包中。 同时在消息的接收方需要通过MyMessage实现的Parcelable接口,将MyMessage重新构造出来。

[java]  view plain copy
  1. import android.os.Parcel;  
  2. import android.os.Parcelable;  
  3.   
  4. public class MyMessage implements Parcelable {  
  5.     private int mData;  
  6.   
  7.     public int describeContents() {  
  8.         return 0;  
  9.     }  
  10.   
  11.     public void writeToParcel(Parcel out, int flags) {  
  12.         out.writeInt(mData);  
  13.     }  
  14.   
  15.     public static final Parcelable.Creator<MyMessage> CREATOR  
  16.            = new  Parcelable.Creator<MyMessage>(){  
  17.         public MyMessage createFromParcel(Parcel in) {  
  18.             return new MyMessage(in);  
  19.         }  
  20.   
  21.         public MyMessage[] newArray(int size) {  
  22.             return new MyMessage[size];  
  23.         }  
  24.     };  
  25.       
  26.     private MyMessage(Parcel in) {  
  27.         mData = in.readInt();  
  28.     }  
  29.   
  30.     public MyMessage(int data) {  
  31.     // TODO Auto-generated constructor stub  
  32.     mData = data;  
  33.     }  
  34. }  
目录
相关文章
|
算法
LeetCode 330. Patching Array
给定一个已排序的正整数数组 nums,和一个正整数 n 。从 [1, n] 区间内选取任意个数字补充到 nums 中,使得 [1, n] 区间内的任何数字都可以用 nums 中某几个数字的和来表示。请输出满足上述要求的最少需要补充的数字个数。
87 0
LeetCode 330. Patching Array
|
机器学习/深度学习 算法框架/工具 TensorFlow
(转) AdversarialNetsPapers
  本文转自:https://github.com/zhangqianhui/AdversarialNetsPapers AdversarialNetsPapers The classical Papers about adversarial nets The First pap...
[LeetCode] Patching Array
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the
978 0
|
存储 容器 Java
SparseArray<E>详解
SparseArray 是官方推荐的用来替代 HashMap 的一个工具类,相比来说有着更好的性能(其核心是折半查找函数(binarySearch))。 通过源码来看下SparseArray的一些使用特点: --------------------------------------------...
950 0

热门文章

最新文章