今天要給大家講一下Android中 Intent中如何傳遞對象,就我目前所知道的有兩種方法,一種是Bundle.putSerializable(Key,Object);另一種是 Bundle.putParcelable(Key, Object);當然這些Object是有一定的條件的,前者是實作了Serializable接口,而後者是實作了Parcelable接口,為了讓大 家更容易了解我還是照常寫了一個簡單的Demo,大家就一步一步跟我來吧!
第一步:建立一個Android工程命名為ObjectTranDemo(類比較多哦!)目錄結構如下圖:
第二步:修改main.xml布局檔案(這裡我增加了兩個按鈕)代碼如下
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr wei's blog."
/>
<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:text="Serializable"
/>
android:id="@+id/button2"
android:text="Parcelable"
</LinearLayout>
第三步:建立兩個類一個是Person.Java實作Serializable接口,另一個Book.java實作Parcelable接口,代碼分别如下:
Person.java:
package com.tutor.objecttran;
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;
}
Book.java:
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;
public static final Parcelable.Creator<Book> CREATOR = new Creator<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];
};
public int describeContents() {
return 0;
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(bookName);
parcel.writeString(author);
parcel.writeInt(publishTime);
第四步:修改ObjectTranDemo.java,并且建立兩個Activity,一個是ObjectTranDemo1.java,别一個是ObjectTranDemo2.java.分别用來顯示Person對像資料,和Book對象資料:,代碼分别如下:
ObjectTranDemo.java:
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 ObjectTranDemo extends Activity implements OnClickListener {
private Button sButton,pButton;
public final static String SER_KEY = "com.tutor.objecttran.ser";
public final static String PAR_KEY = "com.tutor.objecttran.par";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
//我的一貫作風呵呵
public void setupViews(){
sButton = (Button)findViewById(R.id.button1);
pButton = (Button)findViewById(R.id.button2);
sButton.setOnClickListener(this);
pButton.setOnClickListener(this);
//Serializeable傳遞對象的方法
public void SerializeMethod(){
Person mPerson = new Person();
mPerson.setName("frankie");
mPerson.setAge(25);
Intent mIntent = new Intent(this,ObjectTranDemo1.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("Android Tutor");
mBook.setAuthor("Frankie");
mBook.setPublishTime(2010);
Intent mIntent = new Intent(this,ObjectTranDemo2.class);
mBundle.putParcelable(PAR_KEY, mBook);
//铵鈕點選事件響應
public void onClick(View v) {
if(v == sButton){
SerializeMethod();
}else{
PacelableMethod();
ObjectTranDemo1.java:
import android.widget.TextView;
public class ObjectTranDemo1 extends Activity {
@Override
TextView mTextView = new TextView(this);
Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);
mTextView.setText("You name is: " + mPerson.getName() + "/n"+
"You age is: " + mPerson.getAge());
setContentView(mTextView);
ObjectTranDemo2.java:
public class ObjectTranDemo2 extends Activity {
Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);
mTextView.setText("Book name is: " + mBook.getBookName()+"/n"+
"Author is: " + mBook.getAuthor() + "/n" +
"PublishTime is: " + mBook.getPublishTime());
第五步:比較重要的一步啦,修改AndroidManifest.xml檔案(将兩個新增的Activity,ObjectTranDemo1,ObjectTranDemo2)申明一下代碼如下(第14,15行):
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutor.objecttran"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ObjectTranDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ObjectTranDemo1"></activity>
<activity android:name=".ObjectTranDemo2"></activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
第六步:運作上述工程檢視效果圖啦:
效果1:首界面:
效果2:點選Serializable按鈕
效果3:點選Parcelable按鈕:
作者: 一點點征服
本文版權歸作者所有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連結,否則保留追究法律責任的權利