现在的位置: 首页 > 综合 > 正文

Intent如何传递对象

2019年06月06日 ⁄ 综合 ⁄ 共 7153字 ⁄ 字号 评论关闭

Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,为了让大家更容易理解我还是照常写了一个简单的Demo。 

工程结构图: 
[img] 
 
[/img] 

Book: 

Java代码 

 收藏代码

  1. package com.tutor.objecttran;  
  2. import android.os.Parcel;  
  3. import android.os.Parcelable;  
  4. public class Book implements Parcelable {  
  5.     private String bookName;  
  6.     private String author;  
  7.     private int publishTime;  
  8.       
  9.     public String getBookName() {  
  10.         return bookName;  
  11.     }  
  12.     public void setBookName(String bookName) {  
  13.         this.bookName = bookName;  
  14.     }  
  15.     public String getAuthor() {  
  16.         return author;  
  17.     }  
  18.     public void setAuthor(String author) {  
  19.         this.author = author;  
  20.     }  
  21.     public int getPublishTime() {  
  22.         return publishTime;  
  23.     }  
  24.     public void setPublishTime(int publishTime) {  
  25.         this.publishTime = publishTime;  
  26.     }  
  27.       
  28.     public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {  
  29.         public Book createFromParcel(Parcel source) {  
  30.             Book mBook = new Book();  
  31.             mBook.bookName = source.readString();  
  32.             mBook.author = source.readString();  
  33.             mBook.publishTime = source.readInt();  
  34.             return mBook;  
  35.         }  
  36.         public Book[] newArray(int size) {  
  37.             return new Book[size];  
  38.         }  
  39.     };  
  40.       
  41.     public int describeContents() {  
  42.         return 0;  
  43.     }  
  44.     public void writeToParcel(Parcel parcel, int flags) {  
  45.         parcel.writeString(bookName);  
  46.         parcel.writeString(author);  
  47.         parcel.writeInt(publishTime);  
  48.     }  
  49. }  



Person: 

Java代码 

 收藏代码

  1. package com.tutor.objecttran;  
  2. import java.io.Serializable;  
  3. public class Person implements Serializable {  
  4.     private static final long serialVersionUID = -7060210544600464481L;   
  5.     private String name;  
  6.     private int age;  
  7.     public String getName() {  
  8.         return name;  
  9.     }  
  10.     public void setName(String name) {  
  11.         this.name = name;  
  12.     }  
  13.     public int getAge() {  
  14.         return age;  
  15.     }  
  16.     public void setAge(int age) {  
  17.         this.age = age;  
  18.     }  
  19.       
  20. }  



ObjectTranDemo: 

Java代码 

 收藏代码

  1. package com.tutor.objecttran;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. public class ObjectTranDemo extends Activity implements OnClickListener {  
  9.       
  10.     private Button sButton,pButton;  
  11.     public  final static String SER_KEY = "com.tutor.objecttran.ser";  
  12.     public  final static String PAR_KEY = "com.tutor.objecttran.par";  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);     
  16.         setupViews();  
  17.           
  18.     }  
  19.       
  20.     //我的一贯作风呵呵  
  21.     public void setupViews(){  
  22.         sButton = (Button)findViewById(R.id.button1);  
  23.         pButton = (Button)findViewById(R.id.button2);  
  24.         sButton.setOnClickListener(this);  
  25.         pButton.setOnClickListener(this);  
  26.     }  
  27.     //Serializeable传递对象的方法  
  28.     public void SerializeMethod(){  
  29.         Person mPerson = new Person();  
  30.         mPerson.setName("frankie");  
  31.         mPerson.setAge(25);  
  32.         Intent mIntent = new Intent(this,ObjectTranDemo1.class);  
  33.         Bundle mBundle = new Bundle();  
  34.         mBundle.putSerializable(SER_KEY,mPerson);  
  35.         mIntent.putExtras(mBundle);  
  36.           
  37.         startActivity(mIntent);  
  38.     }  
  39.     //Pacelable传递对象方法  
  40.     public void PacelableMethod(){  
  41.         Book mBook = new Book();  
  42.         mBook.setBookName("Android Tutor");  
  43.         mBook.setAuthor("Frankie");  
  44.         mBook.setPublishTime(2010);  
  45.         Intent mIntent = new Intent(this,ObjectTranDemo2.class);  
  46.         Bundle mBundle = new Bundle();  
  47.         mBundle.putParcelable(PAR_KEY, mBook);  
  48.         mIntent.putExtras(mBundle);  
  49.           
  50.         startActivity(mIntent);  
  51.     }  
  52.     //铵钮点击事件响应  
  53.     public void onClick(View v) {  
  54.         if(v == sButton){  
  55.             SerializeMethod();  
  56.         }else{  
  57.             PacelableMethod();  
  58.         }  
  59.     }  
  60. }  



ObjectTranDemo1: 

Java代码 

 收藏代码

  1. package com.tutor.objecttran;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.widget.TextView;  
  5. public class ObjectTranDemo1 extends Activity {  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.           
  10.         TextView mTextView = new TextView(this);  
  11.         Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);  
  12.         mTextView.setText("You name is: " + mPerson.getName() + "/n"+  
  13.                 "You age is: " + mPerson.getAge());  
  14.           
  15.         setContentView(mTextView);  
  16.     }  
  17. }  



ObjectTranDemo2: 

Java代码 

 收藏代码

  1. package com.tutor.objecttran;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.widget.TextView;  
  5. public class ObjectTranDemo2 extends Activity {  
  6.    
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         TextView mTextView = new TextView(this);  
  10.         Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);  
  11.         mTextView.setText("Book name is: " + mBook.getBookName()+"/n"+  
  12.                           "Author is: " + mBook.getAuthor() + "/n" +  
  13.                           "PublishTime is: " + mBook.getPublishTime());  
  14.         setContentView(mTextView);  
  15.     }  
  16. }  



res/layout/main.xml 

Java代码 

 收藏代码

  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="Welcome to Mr wei's blog."  
  11.     />  
  12. <Button  
  13.     android:id="@+id/button1"  
  14.     android:layout_width="fill_parent"  
  15.     android:layout_height="wrap_content"  
  16.     android:text="Serializable"  
  17. />  
  18. <Button  
  19.     android:id="@+id/button2"  
  20.     android:layout_width="fill_parent"  
  21.     android:layout_height="wrap_content"  
  22.     android:text="Parcelable"  
  23. />  
  24. </LinearLayout>  



AndroidManifest.xml文件(将两个新增的Activity,ObjectTranDemo1,ObjectTranDemo2)申明一下 

Java代码 

 收藏代码

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.tutor.objecttran"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".ObjectTranDemo"  
  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=".ObjectTranDemo1"></activity>  
  15.         <activity android:name=".ObjectTranDemo2"></activity>  
  16.     </application>  
  17.     <uses-sdk android:minSdkVersion="7" />  
  18. </manifest>   

抱歉!评论已关闭.