android 數據封裝類-Parcelable 使用和學習

概述

我們在應用層使用會遇到需要進行數據傳輸的時候,通常都是使用的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);
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,517評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,087評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,521評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,493評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,207評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,603評論 1 325
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,624評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,813評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,364評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,110評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,305評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,874評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,532評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,953評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,209評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,033評論 3 396
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,268評論 2 375

推薦閱讀更多精彩內容