Android中Parcelable接口的使用
在做开发的过程中,序列化是非常常见的。比如要将对象保存本地磁盘或者在网络上传输等。实现序列化有两种方式,一种是实现Serializable接口,第二种是实现Parcelable。
Serializable与Parcelable的区别
1、Serializable是JDK提供的接口,而Parcelable是Android SDK提供的。
2、Serializable序列化是基于磁盘的,而Parcelable是基于内存的。在内存中读写肯定效率要高于磁盘,所以Android中跨进程传递对象都是使用Parcelable。
Parcelable接口定义
public interface Parcelable {
//内容描述接口,基本不用管
public int describeContents();
//写入接口函数,打包
public void writeToParcel(Parcel dest, int flags);
//读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入。
//为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例。
public interface Creator<T> {
public T createFromParcel(Parcel source);
public T[] newArray(int size);
}
从parcelable接口定义中,我们可以看到,实现parcelable接口,需要我们实现下面几个方法:
1.describeContents方法。内容接口描述,默认返回0就可以;
2.writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中.即打包需要传递的数据到Parcel容器保存,以便从parcel容器获取数据,该方法声明如下:
writeToParcel (Parcel dest, int flags) 具体参数含义见javadoc
3.静态的Parcelable.Creator接口,本接口有两个方法:
createFromParcel(Parcel in) 从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层。
newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。方法是供外部类反序列化本类数组使用。
Parcelable的使用
public class AppContent implements Serializable, Parcelable {
//应用名字
private String name;
//应用下载链接
private String url;
private int downloadPercent = 0;
private Status status = Status.PENDING;
public AppContent(String name, String url) {
this.name = name;
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getDownloadPercent() {
return downloadPercent;
}
public void setDownloadPercent(int downloadPercent) {
this.downloadPercent = downloadPercent;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
@Override
public String toString() {
return name;
}
@Override
public int describeContents() {
return 0;
}
//实现Parcel接口必须覆盖实现的方法
@Override
public void writeToParcel(Parcel dest, int flags) {
/*将AppContent的成员写入Parcel,
* 注:Parcel中的数据是按顺序写入和读取的,即先被写入的就会先被读取出来
*/
dest.writeString(name);
dest.writeString(url);
dest.writeInt(downloadPercent);
dest.writeValue(status);
}
//该静态域是必须要有的,而且名字必须是CREATOR,否则会出错
public static final Parcelable.Creator<AppContent> CREATOR =
new Parcelable.Creator<AppContent>() {
@Override
public AppContent createFromParcel(Parcel source) {
//从Parcel读取通过writeToParcel方法写入的AppContent的相关成员信息
String name = source.readString();
String url = source.readString();
int downloadPercent = source.readInt();
Status status = (Status)source.readValue(new ClassLoader(){});
AppContent appContent = new AppContent(name, url);
appContent.setDownloadPercent(downloadPercent);
appContent.setStatus(status);
//更加读取到的信息,创建返回Person对象
return appContent;
}
@Override
public AppContent[] newArray(int size)
{
// TODO Auto-generated method stub
//返回AppContent对象数组
return new AppContent[size];
}
};
}
通过Intent进行传递:
Intent intent = new Intent(Constants.DOWNLOAD_MSG);
Bundle bundle = new Bundle();
bundle.putParcelable("appContent", appContent);
intent.putExtras(bundle);
参考:http://www.tuicool.com/articles/MJzAZn,感谢作者。
Android中Parcelable接口的使用的更多相关文章
- Android中Parcelable接口用法
from: http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html Interface for classes wh ...
- Android中Parcelable接口
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- (转)Android中Parcelable接口用法
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- Android中Parcelable与Serializable接口用法
转自: Android中Parcelable接口用法 1. Parcelable接口 Interface for classes whose instances can be written to a ...
- Android中的接口回调技术
Android中的接口回调技术有很多应用的场景,最常见的:Activity(人机交互的端口)的UI界面中定义了Button,点击该Button时,执行某个逻辑. 下面参见上述执行的模型,讲述James ...
- Android中回调接口的使用
MainActivity如下: package cn.testcallback; import android.os.Bundle; import android.app.Activity; /** ...
- Android中Parcelable和Serializable接口用法
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- Android开发中Parcelable接口的使用方法
在网上看到很多Android初入门的童鞋都在问Parcelable接口的使用方法,小编参考了相关Android教程,看到里面介绍的序列化方法主要有两种分别是实现Serializable接口和实现Par ...
- Android 的Parcelable接口
此文转载自http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html 1. Parcelable接口 Interface ...
随机推荐
- Eclipse+Mingw+Boost 环境搭建
一.安装CDT插件 Eclipse是用Java的swt开发的,所以需要java虚拟机才能运行,jdk的配置方法网上一大堆,这里就不细说了.安装CDT的方法简单提一下,到Help->Eclipse ...
- mvc深入理解
对于v和c好理解, 对于model分为数据对象模型和业务逻辑模型,一般为一个类,数据对象模型包含对一个具体数据表的相关操作,业务逻辑模型为处理一些业务逻辑.
- Sql 常见面试题
SQL面试题(1) create table testtable1(id int IDENTITY,department varchar(12) ) select * from testtable1i ...
- stsdb开发指南
摘自:http://www.iopenworks.com/Products/ProductDetails/DevelopmentGuide?proID=387 多线程问题,请参见线程安全小结 1 ST ...
- IOS s数据存储之归档解档
#import <Foundation/Foundation.h> @interface Student : NSObject <NSCoding>; @property(no ...
- string.Format , object[] args 使用
string sql = "insert into TableA values('{0}','{1}','{2}',GetDate(),'{3}' "; sql = string. ...
- PHP中想过获取系统内置的所有常量吗?
print_r(get_defined_constants(true)); OK!这样就可以了,赶紧去试试吧
- PHP接口使用
<?php interface IEmployee { function working(); } interface IDeveloper { function Coding(); } cla ...
- OGLplus 0.33.0 发布,OpenGL 的 C 封装库
OGLplus 0.33.0 引入很多新的 OGLplus 和 OALplus 示例,更新了构建系统.CamMatrix::LookingAt 构造器.Texture::MaxLevel getter ...
- ContentControl 与 ViewModel (二)
上文说到 可以使用DataTemplateSelector. 其实等于是用 DataTemplateSelector + 动态创建DataTemplate来实现. using System; usin ...