【轉】Android中intent傳遞對象和Bundle的用法


原文網址:http://blog.csdn.net/lixiang0522/article/details/8642202

android中的組件間傳遞的對象一般實現Parcelable接口,當然也可以使用java的Serializable接口,前者是android專門設計的,效率更高,下面我們就來實現一個Parcelabel。
 
1. 創建一個類實現Parcelable接口,具體實現如下:
[java]  view plain  copy
 
  1. package com.hebaijun.testparcelable;  
  2.   
  3. import android.os.Parcel;  
  4. import android.os.Parcelable;  
  5.   
  6. public class ParcelableData implements Parcelable{  
  7.     private String name;  
  8.     private int age;  
  9.       
  10.     public ParcelableData(){  
  11.         name = "guest";  
  12.         age = 20;  
  13.     }  
  14.       
  15.     public ParcelableData(Parcel in){  
  16.         //順序要和writeToParcel寫的順序一樣  
  17.         name = in.readString();  
  18.         age = in.readInt();  
  19.     }  
  20.       
  21.     public String getName(){  
  22.         return name;  
  23.     }  
  24.       
  25.     public void setName(String name){  
  26.         this.name = name;  
  27.     }  
  28.       
  29.     public int getAge(){  
  30.         return age;  
  31.     }  
  32.       
  33.     public void setAge(int age) {  
  34.         this.age = age;  
  35.     }  
  36.   
  37.     @Override  
  38.     public int describeContents() {  
  39.         // TODO Auto-generated method stub  
  40.         return 0;  
  41.     }  
  42.   
  43.     @Override  
  44.     public void writeToParcel(Parcel dest, int flags) {  
  45.         // TODO Auto-generated method stub  
  46.         dest.writeString(name);  
  47.         dest.writeInt(age);  
  48.     }  
  49.       
  50.     public static final Parcelable.Creator<ParcelableData> CREATOR = new Parcelable.Creator<ParcelableData>() {  
  51.         public ParcelableData createFromParcel(Parcel in) {  
  52.             return new ParcelableData(in);  
  53.         }  
  54.           
  55.         public ParcelableData[] newArray(int size) {  
  56.             return new ParcelableData[size];  
  57.         }  
  58.     };  
  59.   
  60. }  


2. 通過下面的方法發送對象。Bundle類也實現了Parcelable接口,一般在android中我們是通過Bundle來封裝數據並進行傳送的。
[java]  view plain  copy
 
  1. Intent intent = new Intent();  
  2. intent.setClass(this, SubActivity.class);  
  3. // 直接添加  
  4. //intent.putExtra("MyData", new ParcelableData());  
  5.   
  6. // 通過Bundle  
  7. Bundle bundle = new Bundle();  
  8. bundle.putString("MyString", "test bundle");  
  9. bundle.putParcelable("MyData", new ParcelableData());  
  10. intent.putExtras(bundle);  
  11. startActivity(intent);  


3. 下面的接收對象的方法。
[java]  view plain  copy
 
  1. //ParcelableData parcelableData = getIntent().getParcelableExtra("MyData");  
  2. Bundle bundle = getIntent().getExtras();  
  3. ParcelableData parcelableData = bundle.getParcelable("MyData");  
  4. String testBundleString = bundle.getString("MyString");  
  5. Log.v("string=", testBundleString);  
  6. Log.v("name=", parcelableData.getName());  
  7. Log.v("age=", ""+parcelableData.getAge());  

 

傳輸的對象需要實現序列化:有兩種方式,一種是實現Serializable接口,就是原來的java方式;另外一種是android的Parcelable方式,這個性能可能好一些,我猜的,但是這在需要手動去寫Parcelable接口的實現。

Serializable存數據:

 

  1. Person mPerson = new Person();  
  2.         mPerson.setName("frankie");  
  3.         mPerson.setAge(25);  
  4.         Intent mIntent = new Intent(this,ObjectTranDemo1.class);  
  5.         Bundle mBundle = new Bundle();  
  6.         mBundle.putSerializable(SER_KEY,mPerson);  
  7.         mIntent.putExtras(mBundle); 

 

 

Serializable取數據:

 

 // 獲取啟動該ResultActivity的Intent
24         Intent intent = getIntent();
25         // 獲取該Intent所攜帶的數據
26         Bundle bundle = intent.getExtras();
27         // 從bundle數據包中取出數據
28         Person person = (Person) bundle.getSerializable("person");

Parcelable存數據:

 

 

  1. Intent mIntent = new Intent(this,ObjectTranDemo2.class);  
  2.         Bundle mBundle = new Bundle();  
  3.         mBundle.putParcelable(PAR_KEY, mBook);  
  4.         mIntent.putExtras(mBundle);

Parcelable取數據:

 

 


  1.         Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);

參考1:http://blog.csdn.net/Android_Tutor/article/details/5740845

參考2:http://my.oschina.net/u/577632/blog/76906


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM