Android中Intent中如何传递对象有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,通过一个简单的例子来给大家讲解一下。
工程结构图:
工程结构图:
运行结果截图:
mainActivity截图
SerialActivity截图
ParcelActivity截图
Book.java:
package com.intent.bean;import android.os.Parcel;import android.os.Parcelable;public class Book implements Parcelable { private String bookName; private String author; private int publishTime; public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getPublishTime() { return publishTime; } public void setPublishTime(int publishTime) { this.publishTime = publishTime; } // 实例化静态内部对象CREATOR实现接口Parcelable.Creator public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() { // 将Parcel对象反序列化为Book public Book createFromParcel(Parcel source) { Book mBook = new Book(); mBook.bookName = source.readString(); mBook.author = source.readString(); mBook.publishTime = source.readInt(); return mBook; } public Book[] newArray(int size) { return new Book[size]; } }; @Override public int describeContents() { return 0; } // 实现Parcelable的方法writeToParcel,将Book序列化为一个Parcel对象 @Override public void writeToParcel(Parcel parcel, int flags) { parcel.writeString(bookName); parcel.writeString(author); parcel.writeInt(publishTime); }}
Person.java:
package com.intent.bean;import java.io.Serializable;public class Person implements Serializable { private static final long serialVersionUID = -7060210544600464481L; private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
MainActivity.java:
package com.intent.activity;import com.intent.bean.Book;import com.intent.bean.Person;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener { private Button btn_serial; private Button btn_parcel; public final static String SER_KEY = "com.intent.activity.ser"; public final static String PAR_KEY = "com.intent.activity.par"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } // 我的一贯作风呵呵 public void setupViews() { btn_serial = (Button) findViewById(R.id.btOpenSerialActivity); btn_serial.setOnClickListener(this); btn_parcel = (Button) findViewById(R.id.btOpenParcelActivity); btn_parcel.setOnClickListener(this); } // Serializeable传递对象的方法 public void SerializeMethod() { Person mPerson = new Person(); mPerson.setName("wulianghuan"); mPerson.setAge(22); Intent mIntent = new Intent(this, SerialActivity.class); Bundle mBundle = new Bundle(); mBundle.putSerializable(SER_KEY, mPerson); mIntent.putExtras(mBundle); startActivity(mIntent); } // Pacelable传递对象方法 public void PacelableMethod() { Book mBook = new Book(); mBook.setBookName("Think in java"); mBook.setAuthor("stven"); mBook.setPublishTime(2010); Intent mIntent = new Intent(this, ParcelActivity.class); Bundle mBundle = new Bundle(); mBundle.putParcelable(PAR_KEY, mBook); mIntent.putExtras(mBundle); startActivity(mIntent); } // 铵钮点击事件响应 public void onClick(View v) { if (v == btn_serial) { SerializeMethod(); } else { PacelableMethod(); } }}
SerialActivity.java:
package com.intent.activity;import com.intent.bean.Person;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class SerialActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView mTextView = new TextView(this); Person mPerson = (Person) getIntent().getSerializableExtra(MainActivity.SER_KEY); mTextView.setText("name: " + mPerson.getName() + " , " + "age: " + mPerson.getAge()); setContentView(mTextView); }}
ParcelActivity.java:
package com.intent.activity;import com.intent.bean.Book;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class ParcelActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView mTextView = new TextView(this); Book mBook = (Book) getIntent().getParcelableExtra(MainActivity.PAR_KEY); mTextView.setText("name: " + mBook.getBookName() + " , " + "Author: " + mBook.getAuthor() + " , " + "PublishTime: " + mBook.getPublishTime()); setContentView(mTextView); }}
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btOpenSerialActivity" android:text="使用意图传递Serializable对象"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btOpenParcelActivity" android:text="使用意图传递Parcelable对象"/></LinearLayout>
AndroidManifest.xml文件(将两个新增的Activity,SerialActivity,ParcelActivity)声明一下 :
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.intent.activity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".MainActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SerialActivity"/> </application></manifest>