【移動應用開發(fā)技術】Android中如何使用Parcelable接口_第1頁
【移動應用開發(fā)技術】Android中如何使用Parcelable接口_第2頁
【移動應用開發(fā)技術】Android中如何使用Parcelable接口_第3頁
【移動應用開發(fā)技術】Android中如何使用Parcelable接口_第4頁
【移動應用開發(fā)技術】Android中如何使用Parcelable接口_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

【移動應用開發(fā)技術】Android中如何使用Parcelable接口

Android中如何使用Parcelable接口,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面在下將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

AndroidParcelable接口使用方法詳解1.Parcelable接口InterfaceforclasseswhoseinstancescanbewrittentoandrestoredfromaParcel。ClassesimplementingtheParcelableinterfacemustalsohaveastaticfieldcalledCREATOR,whichisanobjectimplementingtheParcelable.Creatorinterface。2.實現(xiàn)Parcelable就是為了進行序列化,那么,為什么要序列化?1)永久性保存對象,保存對象的字節(jié)序列到本地文件中;2)通過序列化對象在網(wǎng)絡中傳遞對象;3)通過序列化在進程間傳遞對象。3.實現(xiàn)序列化的方法Android中實現(xiàn)序列化有兩個選擇:一是實現(xiàn)Serializable接口(是JavaSE本身就支持的),一是實現(xiàn)Parcelable接口(是android特有功能,效率比實現(xiàn)Serializable接口高效,可用于Intent數(shù)據(jù)傳遞,也可以用于進程間通信(IPC))。實現(xiàn)Serializable接口非常簡單,聲明一下就可以了,而實現(xiàn)Parcelable接口稍微復雜一些,但效率更高,推薦用這種方法提高性能。注:Android中Intent傳遞對象有兩種方法:一是Bundle.putSerializable(Key,Object),另一種是Bundle.putParcelable(Key,Object)。當然這些Object是有一定的條件的,前者是實現(xiàn)了Serializable接口,而后者是實現(xiàn)了Parcelable接口。4.選擇序列化方法的原則1)在使用內存的時候,Parcelable比Serializable性能高,所以推薦使用Parcelable。2)Serializable在序列化的時候會產(chǎn)生大量的臨時變量,從而引起頻繁的GC。3)Parcelable不能使用在要將數(shù)據(jù)存儲在磁盤上的情況,因為Parcelable不能很好的保證數(shù)據(jù)的持續(xù)性在外界有變化的情況下。盡管Serializable效率低點,但此時還是建議使用Serializable。5.應用場景需要在多個部件(Activity或Service)之間通過Intent傳遞一些數(shù)據(jù),簡單類型(如:數(shù)字、字符串)的可以直接放入Intent。復雜類型必須實現(xiàn)Parcelable接口。6、Parcelable接口定義public

interface

Parcelable

{

//內容描述接口,基本不用管

public

int

describeContents();

//寫入接口函數(shù),打包

public

void

writeToParcel(Parcel

dest,

int

flags);

//讀取接口,目的是要從Parcel中構造一個實現(xiàn)了Parcelable的類的實例處理。因為實現(xiàn)類在這里還是不可知的,所以需要用到模板的方式,繼承類名通過模板參數(shù)傳入

//為了能夠實現(xiàn)模板參數(shù)的傳入,這里定義Creator嵌入接口,內含兩個接口函數(shù)分別返回單個和多個繼承類實例

public

interface

Creator<T>

{

public

T

createFromParcel(Parcel

source);

public

T[]

newArray(int

size);

}

}7、實現(xiàn)Parcelable步驟1)implementsParcelable2)重寫writeToParcel方法,將你的對象序列化為一個Parcel對象,即:將類的數(shù)據(jù)寫入外部提供的Parcel中,打包需要傳遞的數(shù)據(jù)到Parcel容器保存,以便從Parcel容器獲取數(shù)據(jù)3)重寫describeContents方法,內容接口描述,默認返回0就可以4)實例化靜態(tài)內部對象CREATOR實現(xiàn)接口Parcelable.Creatorpublic

static

final

Parcelable.Creator<T>

CREATOR注:其中publicstaticfinal一個都不能少,內部對象CREATOR的名稱也不能改變,必須全部大寫。需重寫本接口中的兩個方法:createFromParcel(Parcelin)實現(xiàn)從Parcel容器中讀取傳遞數(shù)據(jù)值,封裝成Parcelable對象返回邏輯層,newArray(intsize)創(chuàng)建一個類型為T,長度為size的數(shù)組,僅一句話即可(returnnewT[size]),供外部類反序列化本類數(shù)組使用。簡而言之:通過writeToParcel將你的對象映射成Parcel對象,再通過createFromParcel將Parcel對象映射成你的對象。也可以將Parcel看成是一個流,通過writeToParcel把對象寫到流里面,在通過createFromParcel從流里讀取對象,只不過這個過程需要你來實現(xiàn),因此寫的順序和讀的順序必須一致。代碼如下:public

class

MyParcelable

implements

Parcelable

{

private

int

mData;

public

int

describeContents()

{

return

0;

}

public

void

writeToParcel(Parcel

out,

int

flags)

{

out.writeInt(mData);

}

public

static

final

Parcelable.Creator<MyParcelable>

CREATOR

=

new

Parcelable.Creator<MyParcelable>()

{

public

MyParcelable

createFromParcel(Parcel

in)

{

return

new

MyParcelable(in);

}

public

MyParcelable[]

newArray(int

size)

{

return

new

MyParcelable[size];

}

};

private

MyParcelable(Parcel

in)

{

mData

=

in.readInt();

}

}8、Serializable實現(xiàn)與Parcelabel實現(xiàn)的區(qū)別1)Serializable的實現(xiàn),只需要implements

Serializable即可。這只是給對象打了一個標記,系統(tǒng)會自動將其序列化。2)Parcelabel的實現(xiàn),不僅需要implements

Parcelabel,還需要在類中添加一個靜態(tài)成員變量CREATOR,這個變量需要實現(xiàn)Parcelable.Creator接口。兩者代碼比較:1)創(chuàng)建Person類,實現(xiàn)Serializablepublic

class

Person

implements

Serializable

{

private

static

final

long

serialVersionUID

=

-7060210544600464481L;

private

String

name;

private

int

age;

public

String

getName()

{

return

name;

}

public

void

setName(String

name)

{

=

name;

}

public

int

getAge()

{

return

age;

}

public

void

setAge(int

age)

{

this.age

=

age;

}

}2)創(chuàng)建Book類,實現(xiàn)Parcelablepublic

class

Book

implements

Parcelable

{

private

String

bookName;

private

String

author;

private

int

publishDate;

public

Book()

{

}

public

String

getBookName()

{

return

bookName;

}

public

void

setBookName(String

bookName)

{

this.bookName

=

bookName;

}

public

String

getAuthor()

{

return

author;

}

public

void

setAuthor(String

author)

{

this.author

=

author;

}

public

int

getPublishDate()

{

return

publishDate;

}

public

void

setPublishDate(int

publishDate)

{

this.publishDate

=

publishDate;

}

@Override

public

int

describeContents()

{

return

0;

}

@Override

public

void

writeToParcel(Parcel

out,

int

flags)

{

out.writeString(bookName);

out.writeString(author);

out.writeInt(publishDate);

}

public

static

final

Parcelable.Creator<Book>

CREATOR

=

new

Creator<Book>()

{

@Override

public

Book[]

newArray(int

size)

{

return

new

Book[size];

}

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論