一、Parcelable类(Android独有的)

简介:Parcelable是一个接口。

作用:是Android提供的序列化接口,实现序列化和反序列化的操作。

二、跨进程使用

步骤一:创建Book类继承Parcelable接口

public class Book implements Parcelable {

    private String mBookName;
private int mBookId;
/**
*准备:创建Book类,并继承Parcelable接口
*/
public Book(int bookId, String bookName) {
mBookId = bookId;
mBookName = bookName;
} @Override
public String toString() {
return mBookId+""+mBookName;
}
}

步骤二:会提示必须重写接口的方法

describeContents():返回当前对象的描素内容,如果含有文件描述符(什么叫文件描述符)则返回1,否则返回0,一般都返回0(所以不用考虑咯)。

writeToParcel(Parcel out,int flags):将对象写入序列化

Parcel out :系统提供的输出流,将成员变量存储到内存中。

int flags:0或1,1表示当前对象需要作为返回值保存(不明白),基本上所有情况都为0,(所以说可以不用考虑咯)

//接上面的代码
@Override
public int describeContents() {
return 0;
}//描述文件,现在只要返回0就行 @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mBookId);
dest.writeString(mBookName);
}//将当前对象写入序列化结构

步骤三:创建反序列化对象 Parcelable.Creator<T>接口:专门用于反序列化

重写该接口的方法:

createFromParcel(Parcel in):系统提供的输入流,从序列化的对象获取数据。

newArray(int size):创建该对象的数组  (暂时感觉没用)

注:反序列化的时候,要按照序列化放入数据的顺序获取数据,否则会收不着值。

//一定需要按照这种格式书写 public static final Parcelable.Creator<Book>  CREATOR
public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>(){ @Override
public Book createFromParcel(Parcel source) {
return new Book(source);
}//获取输入流,反序列化对象 @Override
public Book[] newArray(int size) {
return new Book[0];
}
}; //创建构造方法,实例化对象
private Book (Parcel in){
mBookId = in.readInt();
mBookName = in.readString();
}

3.原理

Parcelable利用Parcel out 将数据存储到内存中,然后通过Parcel in 从内存中获取数据。

三、Intent之间传递Parcelable类(就是各个Activity传递对象的方法)

根据上面创建Parcelable的方式,创建该类,然后应用Intent传输就可以了。

第二章——Parcelable接口的使用(跨进程,Intent传输)的更多相关文章

  1. 第二章 使用接口(Using Interfaces)-书籍翻译

    PDF预览 下载地址 http://files.cnblogs.com/DKSoft/CodingInDelphi.pdf 1.1. 解耦(Decoupling) All through this b ...

  2. Android开发艺术探索——第二章:IPC机制(中)

    Android开发艺术探索--第二章:IPC机制(中) 好的,我们继续来了解IPC机制,在上篇我们可能就是把理论的知识写完了,然后现在基本上是可以实战了. 一.Android中的IPC方式 本节我们开 ...

  3. Android开发当中Parcelable接口的使用

    本文转载于:http://www.2cto.com/kf/201205/132814.html 本文稍微做了些修改 android提供了一种新的类型:Parcel.本类被用作封装数据的容器,封装后的数 ...

  4. 第二章——Serializable的使用(跨进程使用和Intent的传递对象)

    一.Serializable类(JAVA本身具有的) 简介:Serializable是一个接口. 作用:是JAVA提供的序列化接口,实现序列化和反序列化的操作. 二.跨进程使用 1.事前准备 publ ...

  5. Service官方教程(11)Bound Service示例之2-AIDL 定义跨进程接口并通信

    Android Interface Definition Language (AIDL) 1.In this document Defining an AIDL Interface Create th ...

  6. MVC5+EF6 简易版CMS(非接口) 第二章:建数据模型

    目录 简易版CMS后台管理系统开发流程 MVC5+EF6 简易版CMS(非接口) 第一章:新建项目 MVC5+EF6 简易版CMS(非接口) 第二章:建数据模型 MVC5+EF6 简易版CMS(非接口 ...

  7. Android开发艺术探索——第二章:IPC机制(上)

    Android开发艺术探索--第二章:IPC机制(上) 本章主要讲解Android的IPC机制,首先介绍Android中的多进程概念以及多进程开发模式中常见的注意事项,接着介绍Android中的序列化 ...

  8. [转]Windows Shell 编程 第二章 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987893】

    第二章Shell的结构  “Shell 编程”的大伞之下有大量的API函数和COM接口.这个种类繁多的‘命令’集允许你用不同的方法对Windows Shell进行编程.函数和接口并不是两种提供相同功能 ...

  9. Android艺术开发探索——第二章:IPC机制(下)

    Android艺术开发探索--第二章:IPC机制(下) 我们继续来讲IPC机制,在本篇中你将会学习到 ContentProvider Socket Binder连接池 一.使用ContentProvi ...

随机推荐

  1. struts2开发经验小结(method="{1}"等)

    这里的{1}表示接收前面action里通过通配符传来的值,例如你配置的是<action name="*Crud" class="example.Crud" ...

  2. 获取mssqlserver与access数据库插入的当前行的id

    //mssqlserver public static int GetInsertId(string sql) { try { SqlCommand cmd = new SqlCommand(); u ...

  3. PC远程调试移动设备(转载)

    我们在移动端进行前端开发时,会遇到一个让人头痛但不得不面对的问题--调试. 在 PC 机器上,我们有功能强大的 Chrome DevTools.Firebug,即便是老版本的 IE ,我们也可以安装微 ...

  4. linux操作系统下的码农常用工具

    IDE: Pycharm PHPStorm Zend Studio 文本编辑器: VIM Sublime Text 版本管理: svn RapidSVN git git ui 文件对比: Meld D ...

  5. Python信息采集器使用轻量级关系型数据库SQLite

    1,引言Python自带一个轻量级的关系型数据库SQLite.这一数据库使用SQL语言.SQLite作为后端数据库,可以搭配Python建网站,或者为python网络爬虫存储数据.SQLite还在其它 ...

  6. codeforces 519A. A and B and Chess,

    A. A and B and Chess time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. delphi 操作 TWebBrowser 实现自动填表(JQuery脚本与 OleVariant 方法)

    版本:DELPHI XE8 操作交通银行信用卡申请表单(2016-03-23),网址如下: https://creditcardapp.bankcomm.com/applynew/front/appl ...

  8. haproxy hdr和path

    path : string This extracts the request's URL path, which starts at the first slash and ends before ...

  9. Bayesian Formulation on Cooperative Tracking

    Suppose a joint state representing a set of \(N_{n}\) nodes moving in a field\[    \textbf{X}=    \b ...

  10. EasyUI选项卡tab页面处理示例

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...