Android 实现Parcelable接口序列化对象
2014-01-26 14:15
441 查看
关于对象序列化的方法,在Android中常用到的一般有两种,一种是可以通过实现Serializable接口,这也是java语言中常用的序列化方法,别外一种就是实现Parcelable接口,这是android所特有的。这两个接口,实现Serializable接口相对简单,声明一下就可以了,而实现Parcelable接口相对要复杂一些,但是android过程中,效率较Serializable要高一些,所以推荐使用实现Parcelable接口的方法来序列化对象。
通过Parcelable接口序列化对象一般要通过以下几个步骤:
1,声明实现Parcelable接口。
2,覆写writeToParcel方法,将对象序列化为Parcel对象。
3,实现内部Parcelable.Creator<T>对象,覆写createFromParcel方法,来将对象反序列化。
下面贴上Android代码中通过实现Parcelable接口序列化对象的例子:
对象序列化的一个好处,就是可以很方便的通过Intent来传递数据,可以直接传递一个对象,也可以通过putParcelableArrayListExtra方法,直接传递一个序列化对象的数组。
关于对象序列化的方法,在Android中常用到的一般有两种,一种是可以通过实现Serializable接口,这也是java语言中常用的序列化方法,别外一种就是实现Parcelable接口,这是android所特有的。这两个接口,实现Serializable接口相对简单,声明一下就可以了,而实现Parcelable接口相对要复杂一些,但是android过程中,效率较Serializable要高一些,所以推荐使用实现Parcelable接口的方法来序列化对象。
通过Parcelable接口序列化对象一般要通过以下几个步骤:
1,声明实现Parcelable接口。
2,覆写writeToParcel方法,将对象序列化为Parcel对象。
3,实现内部Parcelable.Creator<T>对象,覆写createFromParcel方法,来将对象反序列化。
下面贴上Android代码中通过实现Parcelable接口序列化对象的例子:
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.accounts; import android.os.Parcelable; import android.os.Parcel; import android.text.TextUtils; /** * Value type that represents an Account in the {@link AccountManager}. This object is * {@link Parcelable} and also overrides {@link #equals} and {@link #hashCode}, making it * suitable for use as the key of a {@link java.util.Map} */ public class Account implements Parcelable { public final String name; public final String type; public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Account)) return false; final Account other = (Account)o; return name.equals(other.name) && type.equals(other.type); } public int hashCode() { int result = 17; result = 31 * result + name.hashCode(); result = 31 * result + type.hashCode(); return result; } public Account(String name, String type) { if (TextUtils.isEmpty(name)) { throw new IllegalArgumentException("the name must not be empty: " + name); } if (TextUtils.isEmpty(type)) { throw new IllegalArgumentException("the type must not be empty: " + type); } this.name = name; this.type = type; } public Account(Parcel in) { this.name = in.readString(); this.type = in.readString(); } public int describeContents() { return 0; } public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeString(type); } public static final Creator<Account> CREATOR = new Creator<Account>() { public Account createFromParcel(Parcel source) { return new Account(source); } public Account[] newArray(int size) { return new Account[size]; } }; public String toString() { return "Account {name=" + name + ", type=" + type + "}"; } }
对象序列化的一个好处,就是可以很方便的通过Intent来传递数据,可以直接传递一个对象,也可以通过putParcelableArrayListExtra方法,直接传递一个序列化对象的数组。
相关文章推荐
- android开发中对象的序列化-实现Parcelable接口.
- 【android实战经验】实现Parcelable接口进行对象序列化
- 【Android 】序列化对象接口Parcelable使用方法
- Android实现Parcelable对象序列化的实例
- Android 对象序列化 Java中实现Serializable序列化与Android特有的实现Parceable接口序列化操作
- Android中使用Parcelable实现对象的序列化
- Android实现Parcelable对象序列化的实例
- Android 实现序列化:Parcelable和Serializable接口的用法
- Android开发:使用序列化接口Parcelable、Serializable实现Activity间传递复杂数据类型参数
- android Parcelable接口序列化对象
- Android 序列化对象接口Parcelable使用方法
- Android开发:使用序列化接口Parcelable、Serializable实现Activity间传递复杂数据类型参数
- Android开发:使用序列化接口Parcelable、Serializable实现Activity间传递复杂数据类型参数
- [Android] Parcelable接口实现序列化
- Android基础——序列化机制(实现Parcelable接口)
- Android对象实现Parcelable接口产生的异常
- Android 通过(Parcelable)序列化对象实现Activity间传递对象
- Android-Parcelable理解与使用(对象序列化)
- Android 使用Parcelable序列化对象
- 自动化快速实现Parcelable接口序列化