现在的位置: 首页 > 综合 > 正文

Android 实现Parcelable接口序列化对象

2014年06月07日 ⁄ 综合 ⁄ 共 2439字 ⁄ 字号 评论关闭


        关于对象序列化的方法,在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方法,直接传递一个序列化对象的数组。

抱歉!评论已关闭.