概述
我們在應用層使用會遇到需要進行數據傳輸的時候,通常都是使用的intent方式,如果使用基本點的數據類型(String\int\boolean)都是可以直接傳的,但是如果傳輸的數據是一個類的話,下面的方式就不行了。Parcelable在android當中通常用的intent和binder通信當中,通常通過讀和寫的方式來進行服務器和客戶端的通信(數據傳輸)。這個是android專有的類型
//在第一個Activity中向Intent中加入內容String data = "Hello,SecondActivity.class");
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("key","我的數據");
startActivity(intent);
//在第二個Activity中,取出Intent中的內容
Intent intent = getIntent();
String data = intent.getStringExtra("alarm");
在java當中也是有這么一種類型的叫Serializable,它是javaSE本身就支持的序列化接口,但是使用這個接口來進行序列化有一個缺點,因為這個的序列化和樊序列化也就是讀寫需要達大量的IO操作,從而是的效率大大降低,但它也是有優點的就是使用起來非常的方便,只要implement Serializable即可,細聽會自動幫你序列化和反序列化, 所以魚和熊掌不可兼得。所以android設計了另外一個接口Parcelable。parcelable定義了把數據寫入parcel和從parcel讀出數據的接口,一個類的實例,如果需要封裝到消息中去,就必須實現這一接口,如果實現了這個接口,該類的實例就是可以“被打包”。parcelable使用起來就相對麻煩一些。
這個時候我們也會問為什么要序列化呢,序列化能給我們帶來什么好處呢?
- 永久行的保存對象,保存對象的字節的序列到本地文件道中;
- 通過序列化對象在網絡中傳輸
- 通過序列化對象在進程間通信
看看 Parcelable 接口里面是什么東東,
public interface Parcelable
{
//內容描述接口,可以不做任何操作,直接return 0即可;
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步驟
- 首先 必須要 implement Parcelable
- 重寫describeContents方法,內容接口描述,默認返回0就可以
- 重寫writeToParcel 方法,將你的對象序列化為一個Parcel對象,即:將類的數據寫入外部提供的Parcel中,打包需要傳遞的數 據到Parcel容器保存,以便從 Parcel容器獲取數據;
- 實例化靜態內部對象CREATOR實現接口Parcelable.Creator:
實現了的例子
package com.xxx.storybox.model;
import android.os.Parcel;
import android.os.Parcelable;
public class ApplicationMusicItem implements Parcelable {
public static final int MUSIC_TYPE_LOCAL = 0;
public static final int MUSIC_TYPE_REMOTE = 1;
public static final int MUSIC_TYPE_CLOUD = 2;
// common
protected int type;
protected String title;
protected String url;
protected String artist;
protected String album;
protected String albumArt;
protected String genre;
protected int duration;
protected boolean isChecked = false;
protected static int mMemberSize = 9;
public ApplicationMusicItem() {
}
public static int memberSize() {
return mMemberSize;
}
public void setType(int type) {
this.type = type;
}
public int getType() {
return type;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getArtist() {
return artist;
}
public void setAlbum(String album) {
this.album = album;
}
public String getAlbumArt() {
return albumArt;
}
public void setAlbumArt(String albumArt) {
this.albumArt = albumArt;
}
public String getAlbum() {
return album;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getGenre() {
return genre;
}
public void setDuration(int duration) {
this.duration = duration;
}
public int getDuration() {
return duration;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public static int transactionMemberSize() {
return mMemberSize;
}
public static ApplicationMusicItem createFromTransactionArray(String[] array) {
ApplicationMusicItem entry = new ApplicationMusicItem();
entry.title = array[0];
entry.url = array[1];
entry.artist = array[2];
entry.album = array[3];
entry.albumArt = array[4];
entry.genre = array[5];
entry.duration = Integer.valueOf(array[6]);
return entry;
}
public String[] toTransactionArray() {
String[] array = new String[transactionMemberSize()];
array[0] = title;
array[1] = url;
array[2] = artist;
array[3] = album;
array[4] = albumArt;
array[5] = genre;
array[6] = Integer.toString(duration);
return array;
}
protected ApplicationMusicItem(Parcel in) {
this();
type = in.readInt();
title = in.readString();
artist = in.readString();
album = in.readString();
genre = in.readString();
duration = in.readInt();
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(type);
out.writeString(title);
out.writeString(url);
out.writeString(artist);
out.writeString(album);
out.writeString(albumArt);
out.writeInt(duration);
out.writeString(genre);
out.writeString(albumArt);
}
public static final Creator<ApplicationMusicItem> CREATOR = new Creator<ApplicationMusicItem>() {
public ApplicationMusicItem createFromParcel(Parcel in) {
return new ApplicationMusicItem(in);
}
public ApplicationMusicItem[] newArray(int size) {
return new ApplicationMusicItem[size];
}
};
@Override
public String toString() {
return "MusicItem{" +
"type=" + type +
", title='" + title + '\'' +
", url='" + url + '\'' +
", artist='" + artist + '\'' +
", album='" + album + '\'' +
", albumArt='" + albumArt + '\'' +
", genre='" + genre + '\'' +
", duration=" + duration +
", isChecked=" + isChecked +
'}';
}
}
如何使用:
Intent intent = new Intent(A.class,B.class);
Bundle bundle = new Bundle();
bundle.putParcelable("contentNavInfo", contentNavInfo);
intent.putExtras(bundle);