Android開發(fā)之Parcel機(jī)制實(shí)例分析
本文實(shí)例講述了Android開發(fā)之Parcel機(jī)制。分享給大家供大家參考。具體分析如下:
在java中,有序列化機(jī)制。但是在安卓設(shè)備上,由于內(nèi)存有限,所以設(shè)計(jì)了新的序列化機(jī)制。
Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the generalParcelable interface), and references to liveIBinder objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.
Parcel is not a general-purpose serialization mechanism. This class (and the correspondingParcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.
從上面的官方解釋可以看到,Parcel主要就是用來序列化,在一端編碼,在另外一端進(jìn)行解碼。
本質(zhì)上把它當(dāng)成一個(gè)Serialize就可以了,只是它是在內(nèi)存中完成的序列化和反序列化,利用的是連續(xù)的內(nèi)存空間,因此會(huì)更加高效。
我們接下來要說的是Parcel類如何應(yīng)用。就應(yīng)用程序而言,最常見使用Parcel類的場景就是在Activity間傳遞數(shù)據(jù)。沒錯(cuò),在Activity間使用Intent傳遞數(shù)據(jù)的時(shí)候,可以通過Parcelable機(jī)制傳遞復(fù)雜的對(duì)象。
具體例子可以參見這里,寫的很好。
在實(shí)現(xiàn)Parcelable接口的時(shí)候,必須實(shí)現(xiàn)其中的兩個(gè)方法并且定義一個(gè)CREATOR:
@Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(color); }
其中,writeToParcel方法定義了怎么向序列化中寫入該類對(duì)象的信息。
CREATOR對(duì)象中定義了兩個(gè)函數(shù):
public MyColor createFromParcel(Parcel in) { return new MyColor(in); } public MyColor[] newArray(int size) { return new MyColor[size]; }
其中,createFromParcel方法告訴平臺(tái)如何從已經(jīng)序列化的對(duì)象中構(gòu)建該類的實(shí)例。newArray方法的作用不明。實(shí)現(xiàn)于Parcelable接口的CREATOR成員的createFromParcel方法用于告訴平臺(tái)如何從包裹里創(chuàng)建該類的實(shí)例,而writeToParcel方法則用于告訴平臺(tái)如何將該類的實(shí)例存儲(chǔ)到包裹中。通過這種約定,平臺(tái)就知道怎么序列化和反序列化了。
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android自定義View實(shí)現(xiàn)彈性小球效果
前段時(shí)間看到一個(gè)功能,是一個(gè)小球沿著固定軌跡彈動(dòng)的效果,那么這篇文章小編給大家分享在Android中如何自定義View來實(shí)現(xiàn)彈性小球的效果,有需要的可以參考借鑒。2016-09-09Android使用Rotate3dAnimation實(shí)現(xiàn)3D旋轉(zhuǎn)動(dòng)畫效果的實(shí)例代碼
利用Android的ApiDemos的Rotate3dAnimation實(shí)現(xiàn)了個(gè)圖片3D旋轉(zhuǎn)的動(dòng)畫,圍繞Y軸進(jìn)行旋轉(zhuǎn),還可以實(shí)現(xiàn)Z軸的縮放。點(diǎn)擊開始按鈕開始旋轉(zhuǎn),點(diǎn)擊結(jié)束按鈕停止旋轉(zhuǎn)。2018-05-05Android Bitmap的截取及狀態(tài)欄的隱藏和顯示功能
Bitmap是Android系統(tǒng)中的圖像處理中最重要類之一。Bitmap可以獲取圖像文件信息,對(duì)圖像進(jìn)行剪切、旋轉(zhuǎn)、縮放,壓縮等操作,并可以以指定格式保存圖像文件。這篇文章主要介紹了Android Bitmap的截取及狀態(tài)欄的隱藏和顯示功能,需要的朋友可以參考下2017-11-11淺析Android手機(jī)衛(wèi)士接收短信指令執(zhí)行相應(yīng)操作
通過廣播接收者,接收到短信,對(duì)短信內(nèi)容進(jìn)行判斷,如果為我們指定的值就執(zhí)行相應(yīng)的操作。本文給大家介紹Android手機(jī)衛(wèi)士接收短信指令執(zhí)行相應(yīng)操作,感興趣的朋友參考下吧2016-04-04Android程序靜默安裝安裝后重新啟動(dòng)APP的方法
這篇文章主要介紹了Android 靜默安裝,安裝后重新啟動(dòng)APP的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01Android Intent發(fā)送廣播消息實(shí)例詳解
這篇文章主要介紹了Android Intent發(fā)送廣播消息實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04Kotlin數(shù)據(jù)存儲(chǔ)方式全面總結(jié)講解
在開發(fā)過程中,數(shù)據(jù)存取是較為頻繁的,今天我們來了解下android幾種常見的數(shù)據(jù)存取方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-12-12Android實(shí)現(xiàn)仿微信tab高亮icon粘著手的滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)仿微信tab高亮icon粘著手的滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08