Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]

来源:互联网 发布:传奇3数据库修改工具 编辑:程序博客网 时间:2024/04/29 04:01

http://blog.csdn.net/cjjky/article/details/6441104

Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]

7934人阅读 评论(6)收藏 举报
androidlayoutstringbuttonobjectclass

      在Android中的不同Activity之间传递对象,我们可以考虑采用Bundle.putSerializable(Key,Object);也可以考虑采用Bundle.putParcelable(Key, Object);其中前面一种方法中的Object要实现Serializable接口,后面一种方法中的Object要实现Parcelable接口。下面我们以一个完整的例子来说明。

1.新建一个Android的工程,其中该工程的目录结构如下图:

 

2. 修改main.xml布局文件。布局文件的源码如下:

[c-sharp] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12.     <Button  
  13.        android:id="@+id/serButton"  
  14.        android:layout_width="fill_parent"  
  15.        android:layout_height="wrap_content"  
  16.        android:text="Serializable"/>  
  17.     <Button  
  18.        android:id="@+id/parButton"  
  19.        android:layout_width="fill_parent"  
  20.        android:layout_height="wrap_content"  
  21.        android:text="Parcelable"/>  
  22. </LinearLayout>  

3.在工程的src目录下新建一个实体类包,命名为com.andy.entity.同时在该package中添加两个实体类,一个是Person.java,该类实现Serializable接口;一个是Police.java,该类实现Parcelable接口。代码分别如下:

Person.java:

[c-sharp] view plaincopy
  1. package com.andy.entity;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Person implements Serializable {  
  6.       
  7.     private static final long serialVersionUID = -6919461967497580385L;  
  8.       
  9.     private String name;  
  10.     private int age;  
  11.       
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.     public int getAge() {  
  19.         return age;  
  20.     }  
  21.     public void setAge(int age) {  
  22.         this.age = age;  
  23.     }  
  24. }  

Police.java:

[c-sharp] view plaincopy
  1. package com.andy.entity;  
  2.   
  3. import android.os.Parcel;  
  4. import android.os.Parcelable;  
  5.   
  6. public class Police implements Parcelable {  
  7.       
  8.     private String name;  
  9.     private int workTime;  
  10.   
  11.     public String getName() {  
  12.         return name;  
  13.     }  
  14.   
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.   
  19.     public int getWorkTime() {  
  20.         return workTime;  
  21.     }  
  22.   
  23.     public void setWorkTime(int workTime) {  
  24.         this.workTime = workTime;  
  25.     }  
  26.       
  27.     public static final Parcelable.Creator<Police> CREATOR = new Creator<Police>() {  
  28.   
  29.         @Override  
  30.         public Police createFromParcel(Parcel source) {  
  31.             Police police = new Police();  
  32.             police.name = source.readString();  
  33.             police.workTime = source.readInt();  
  34.             return police;  
  35.         }  
  36.   
  37.         @Override  
  38.         public Police[] newArray(int size) {  
  39.             return new Police[size];  
  40.         }  
  41.     };  
  42.   
  43.     @Override  
  44.     public int describeContents() {  
  45.         return 0;  
  46.     }  
  47.   
  48.     @Override  
  49.     public void writeToParcel(Parcel parcel, int flags) {  
  50.         parcel.writeString(name);  
  51.         parcel.writeInt(workTime);  
  52.     }  
  53. }  

4.在包com.andy.testdemo中修改TestActivity.java类,同时在该包中添加类SerializableDemo和ParcelableDemo,分别继承了Activity类和分别显示Person对象和Police对象的数据。代码如下:

[c-sharp] view plaincopy
  1. package com.andy.testdemo;  
  2.   
  3. import com.andy.entity.Person;  
  4. import com.andy.entity.Police;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11.   
  12. public class TestActivity extends Activity {  
  13.     private Button sButton,pButton;  
  14.     public final static String SER_KEY = "com.andy.ser";  
  15.     public final static String PAR_KEY = "com.andy.par";  
  16.       
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.           
  23.         sButton = (Button)findViewById(R.id.serButton);  
  24.         sButton.setOnClickListener(new View.OnClickListener() {  
  25.               
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 SerializeMethod();  
  29.             }  
  30.         });  
  31.           
  32.         pButton = (Button)findViewById(R.id.parButton);  
  33.         pButton.setOnClickListener(new View.OnClickListener() {  
  34.               
  35.             @Override  
  36.             public void onClick(View v) {  
  37.                 PacelableMethod();  
  38.             }  
  39.         });  
  40.     }  
  41.       
  42.     /** 
  43.      * Serializeable传递对象的方法 
  44.      */  
  45.     private void SerializeMethod(){  
  46.         Person mPerson = new Person();     
  47.         mPerson.setName("andy");     
  48.         mPerson.setAge(26);     
  49.         Intent mIntent = new Intent(this,SerializableDemo.class);     
  50.         Bundle mBundle = new Bundle();     
  51.         mBundle.putSerializable(SER_KEY,mPerson);     
  52.         mIntent.putExtras(mBundle);     
  53.           
  54.         startActivity(mIntent);     
  55.     }  
  56.       
  57.     /** 
  58.      * Pacelable传递对象方法  
  59.      */  
  60.     private void PacelableMethod(){  
  61.         Police mPolice = new Police();     
  62.         mPolice.setName("I am Police");        
  63.         mPolice.setWorkTime(2008);     
  64.         Intent mIntent = new Intent(this,ParcelableDemo.class);     
  65.         Bundle mBundle = new Bundle();     
  66.         mBundle.putParcelable(PAR_KEY, mPolice);     
  67.         mIntent.putExtras(mBundle);     
  68.        
  69.         startActivity(mIntent);     
  70.     }  
  71. }  

SerializableDemo.java类

[c-sharp] view plaincopy
  1. package com.andy.testdemo;  
  2.   
  3. import com.andy.entity.Person;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.widget.TextView;  
  8.   
  9. public class SerializableDemo extends Activity {  
  10.   
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.           
  15.         TextView mTextView = new TextView(this);     
  16.         Person mPerson = (Person)getIntent().getSerializableExtra(TestActivity.SER_KEY);     
  17.         mTextView.setText("You name is: " + mPerson.getName() + "/n"+     
  18.                           "You age is: " + mPerson.getAge());     
  19.   
  20.         setContentView(mTextView);     
  21.     }  
  22.   
  23. }  

ParcelableDemo.java类:

[c-sharp] view plaincopy
  1. package com.andy.testdemo;  
  2.   
  3. import com.andy.entity.Police;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.widget.TextView;  
  8.   
  9. public class ParcelableDemo extends Activity {  
  10.   
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.           
  15.         TextView mTextView = new TextView(this);     
  16.         Police mPolice = (Police)getIntent().getParcelableExtra(TestActivity.PAR_KEY);     
  17.         mTextView.setText("Police name is: " + mPolice.getName()+"/n"+     
  18.                           "WorkTime is: " + mPolice.getWorkTime() + "/n");     
  19.         setContentView(mTextView);     
  20.     }  
  21.   
  22. }  

5.在AndroidManifest.xml文件中为新添加的两个Activity进行注册。

[c-sharp] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.andy.testdemo"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".TestActivity"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.         <activity android:name=".SerializableDemo"/>  
  15.         <activity android:name=".ParcelableDemo"/>  
  16.     </application>  
  17.     <uses-sdk android:minSdkVersion="8" />  
  18.   
  19. </manifest>   

6.运行程序查看效果图:

【1】主界面截图:

【2】点击Serializable按钮的效果

【3】点击Parcelable按钮的效果

 

=========================================================================

以上是如何采用Intent在不同的Activity之间传递对象的例子。


0 0
原创粉丝点击