Android开发中Parcelable接口的使用方法
在网上看到很多Android初入门的童鞋都在问Parcelable接口的使用方法,小编参考了相关Android教程,看到里面介绍的序列化方法主要有两种分别是实现Serializable接口和实现Parcelable接口,通过具体试验,和大家分享一下Parcelable接口的用法,希望对大家有帮助。
Serializable接口与Parcelable接口的区别
在将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进行传递:
1 Intent intent = new Intent(Constants.DOWNLOAD_MSG);
2 Bundle bundle = new Bundle();
3 bundle.putParcelable("appContent", appContent);
4 intent.putExtras(bundle);
以上就是Android开发中,序列化常用的Parcelable接口方法,遇到有类似问题的亲可尝试使用上述的方法进行解决。
Android开发中Parcelable接口的使用方法的更多相关文章
- Android开发当中Parcelable接口的使用
本文转载于:http://www.2cto.com/kf/201205/132814.html 本文稍微做了些修改 android提供了一种新的类型:Parcel.本类被用作封装数据的容器,封装后的数 ...
- 【转】Android开发中让你省时省力的方法、类、接口
转载 http://www.toutiao.com/i6362292864885457410/?tt_from=mobile_qq&utm_campaign=client_share& ...
- Android开发中使用startActivityForResult()方法从Activity A跳转Activity B出现B退出时A也同时退出的解决办法
最近一个 App 中用到了 startActivityForResult() 方法,使用的时候却出现了一些问题,比如我在 Activity A 中调用该方法向 Activity B 中跳转,如果 B ...
- Android开发中,那些让您觉得相见恨晚的方法、类或接口
Android开发中,那些让你觉得相见恨晚的方法.类或接口本篇文章内容提取自知乎Android开发中,有哪些让你觉得相见恨晚的方法.类或接口?,其实有一部是JAVA的,但是在android开发中也算常 ...
- 在Android开发中,定时器一般有以下3种实现方法
在Android开发中,定时器一般有以下3种实现方法: 原文地址http://www.360doc.com/content/12/0619/13/87000_219180978.shtml 一.采用H ...
- 在Android开发中,定时执行任务的3种实现方法
在Android开发中,定时执行任务的3种实现方法: 一.采用Handler与线程的sleep(long)方法(不建议使用,Java的实现方式)二.采用Handler的postDelayed(Runn ...
- 在android开发中使用multdex的方法-IT蓝豹为你整理
Android系统在安装应用时,往往需要优化Dex,而由于处理工具DexOpt对id数目的限制,导致其处理的数目不能超过65536个,因此在Android开发中,需要使用到MultiDex来解决这个问 ...
- android开发中关于继承activity类中方法的调用
android开发中关于继承activity类中的函数,不能在其他类中调用其方法. MainActivity.java package com.example.testmain; import and ...
- 在Android开发中替换资源图片不起作用的解决方法
现象 在android开发中,经常会需要替换res\drawable中的图片,打开res\layout下的文件预览布局页面发现图片已经被替换,但在模拟器或者真实机器上运行时发现该图片并没有被替换,还是 ...
随机推荐
- git(osChina上分支的使用)
使用osChina分支的创建分为两种 1.直接在osChina上创建 需要pull否则查看git的状态是不包含改分支的; git pull <git地址/git简称> <分支名> ...
- [转]C++ explicit的作用
explicit作用: 在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换. explicit使用注意事项: * e ...
- Java反射+注解案例
注解类代码,注解的属性可以有多个: package reflect; import java.lang.annotation.Retention; import java.lang.annotatio ...
- Win10上Anaconda环境下python3.6安装和使用pyinstaller
一.安装步骤 1. 电脑是win10,安装的Python3.6 2. 在Scripts文件夹下执行pip install pyinstaller, 安装成功后下载pyinstaller安装包,解压之后 ...
- gdb-pada调试实例
先编写个简单的hello的程序 hello.c (ps:有没有头文件行不行,试试不就知道了) int main(){ printf("hello!\n"); int m,n; in ...
- 通过Samba实现Linux与Windows间的文件共享
Samba Samba,是用来让Linux系列的操作系统与Windows操作系统的SMB/CIFS(Server Message Block/Common Internet File System)网 ...
- 华为模拟器ensp安装教程
华为模拟器说实话有时候真的是很烦人,总是莫名其妙的出问题,而且网上教程一般也解决不了 因此我认为学会ensp的重装真的很重要,因此只要我们删除干净了,安装最多花不了20分钟的时间 接下来我就来说说怎么 ...
- Linux基础知识与命令1(su passwd)
一.Linux的基本原则 1.linux由一个个目的单一的小程序组成,我们一般需要组合小程序来完成复杂的任务 2.Linux的一切都是文件(文件类似于一棵树,包括外设,接口) 3.Linux尽量避免捕 ...
- Codeforces Round #392 (Div. 2) Unfair Poll
C. Unfair Poll time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- C语言进阶——goto 和 void 的分析08
遭人遗弃的goto: 高手潜规则:禁止使用goto 项目经验:程序质量与goto的出现次数成反比 最后的判决:将goto打入冷宫 程序示例1:(goto副作用分析) #include <stdi ...