隨筆 - 35  文章 - 21  trackbacks - 0
          <2011年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜


          android 中自定義的對象序列化的問題有兩個選擇一個是Parcelable,另外一個是Serializable。

          一 序列化原因:

          1.永久性保存對象,保存對象的字節序列到本地文件中;
          2.通過序列化對象在網絡中傳遞對象;
          3.通過序列化在進程間傳遞對象。 

          二 至于選取哪種可參考下面的原則:

          1.在使用內存的時候,Parcelable 類比Serializable性能高,所以推薦使用Parcelable類。
          2.Serializable在序列化的時候會產生大量的臨時變量,從而引起頻繁的GC。
          3.Parcelable不能使用在要將數據存儲在磁盤上的情況,因為Parcelable不能很好的保證數據的持續性在外界有變化的情況下。盡管Serializable效率低點, 也不提倡用,但在這種情況下,還是建議你用Serializable 。


          實現:
          1 Serializable 的實現,只需要繼承  implements Serializable 即可。這只是給對象打了一個標記,系統會自動將其序列化。

          2 Parcelabel 的實現,需要在類中添加一個靜態成員變量 CREATOR,這個變量需要繼承 Parcelable.Creator 接口。
          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();
               }
           }

           
          posted on 2011-09-16 16:16 lincode 閱讀(22139) 評論(0)  編輯  收藏 所屬分類: android
          主站蜘蛛池模板: 黄石市| 北海市| 丽江市| 岗巴县| 乡宁县| 周宁县| 如东县| 逊克县| 舒兰市| 宕昌县| 修武县| 香河县| 永济市| 石柱| 钟山县| 富锦市| 彰化县| 甘德县| 庐江县| 富源县| 南投市| 芮城县| 郯城县| 古浪县| 福安市| 红河县| 贵定县| 乌恰县| 酒泉市| 丰县| 晋中市| 望都县| 平遥县| 大连市| 苏尼特左旗| 遂溪县| 上犹县| 原平市| 重庆市| 津市市| 陵川县|