Android 中Parcelable的作用

來源:互聯網
上載者:User

轉自: http://blog.sina.com.cn/s/blog_78e3ae430100pxba.html

android提供了一種新的類型:Parcel。本類被用作封裝資料的容器,封裝後的資料可以通過Intent或IPC傳遞。 除了基本類型以外,只有實現了Parcelable介面的類才能被放入Parcel中。 Parcelable實現要點:需要實現三個東西1)writeToParcel 方法。該方法將類的資料寫入外部提供的Parcel中.聲明如下:writeToParcel (Parcel dest, int flags) 具體參數含義見javadoc2)describeContents方法。沒搞懂有什麼用,反正直接返回0也可以3)靜態Parcelable.Creator介面,本介面有兩個方法:createFromParcel(Parcel in) 實現從in中建立出類的執行個體的功能newArray(int size) 建立一個類型為T,長度為size的數組,僅一句話(return new T[size])即可。估計本方法是供外部類還原序列化本類數組使用。測試用的接收資訊Activityimport android.app.Activity;    import android.content.Intent;    import android.os.Bundle;   import android.os.Parcelable;     public class Test extends Activity {             @Override      public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);           Intent i = getIntent();           Person p = i.getParcelableExtra("yes");           System.out.println("---->"+p.name);           System.out.println("---->"+p.map.size());       }   }  發送的Activityimport java.util.HashMap;     import android.app.Activity;   import android.content.Intent;   import android.os.Bundle;     public class TestNew extends Activity {             @Override      public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);           Intent intent = new Intent();           Person p = new Person();           p.map = new HashMap<String,String>();           p.map.put("yes", "ido");           p.name="ok";           intent.putExtra("yes", p);           intent.setClass(this, Test.class);           startActivity(intent);       }   }  Parcelable的實作類別import java.util.HashMap;     import android.os.Parcel;   import android.os.Parcelable;     public class Person implements Parcelable {         public HashMap<String,String> map = new HashMap<String,String> ();              public String name ;       @Override      public int describeContents() {           return 0;       }         @Override      public void writeToParcel(Parcel dest, int flags) {             dest.writeMap(map);           dest.writeString(name);       }       public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {   //重寫Creator          @Override          public Person createFromParcel(Parcel source) {               Person p = new Person();               p.map=source.readHashMap(HashMap.class.getClassLoader());               p.name=source.readString();               return p;           }             @Override          public Person[] newArray(int size) {               // TODO Auto-generated method stub               return null;           }       };     

}   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.