Android系列之Intent传递对象的几种实例方法
在android中intent传递对象主要有2种方式分别是,bundle.putserializable(key,object)和bundle.putparcelable(key, object);当然这些object是有一定的条件的,前者是实现了serializable接口,而后者是实现了parcelable接口,以下是我为大家做的一个实例
首先我们建立一个工程项目命名为:objecttestdemo
然后我们再修改main.xml布局文件,主要增加2个按钮
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 jesson's blog."
/>
< button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="serializable"
/>
< button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="parcelable"
/>
< /linearlayout>
< ?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 jesson's blog."
/>
< button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="serializable"
/>
< button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="parcelable"
/>
< /linearlayout>
[code]
接下来我们开始对工程进行实现,分别建立person.java实现serializable接口,另一个book.java实现parcelable接口
[code]
package com.test.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;
}
}
package com.test.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;
}
}
package com.test.tutor.objecttran;
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 creator = new creator() {
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);
}
}
package com.test.tutor.objecttran;
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 creator = new creator() {
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对象数据
代码
package com.test.tutor.objecttran;
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);
bundle mbundle = new bundle();
mbundle.putparcelable(par_key, mbook);
mintent.putextras(mbundle);
startactivity(mintent);
}
//铵钮点击事件响应
public void onclick(view v) {
if(v == sbutton){
serializemethod();
}else{
pacelablemethod();
}
}
}
代码
package com.test.tutor.objecttran;
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);
bundle mbundle = new bundle();
mbundle.putparcelable(par_key, mbook);
mintent.putextras(mbundle);
startactivity(mintent);
}
//铵钮点击事件响应
public void onclick(view v) {
if(v == sbutton){
serializemethod();
}else{
pacelablemethod();
}
}
}
代码
package com.test.tutor.objecttran;
import android.app.activity;
import android.os.bundle;
import android.widget.textview;
public class objecttrandemo1 extends activity {
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
textview mtextview = new textview(this);
person mperson = (person)getintent().getserializableextra(objecttrandemo.ser_key);
mtextview.settext("you name is: " + mperson.getname() + ""+
"you age is: " + mperson.getage());
setcontentview(mtextview);
}
代码
package com.test.tutor.objecttran;
import android.app.activity;
import android.os.bundle;
import android.widget.textview;
public class objecttrandemo1 extends activity {
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
textview mtextview = new textview(this);
person mperson = (person)getintent().getserializableextra(objecttrandemo.ser_key);
mtextview.settext("you name is: " + mperson.getname() + ""+
"you age is: " + mperson.getage());
setcontentview(mtextview);
}
}
代码
package com.test.tutor.objecttran;
import android.app.activity;
import android.os.bundle;
import android.widget.textview;
public class objecttrandemo2 extends activity {
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
textview mtextview = new textview(this);
book mbook = (book)getintent().getparcelableextra(objecttrandemo.par_key);
mtextview.settext("book name is: " + mbook.getbookname()+""+
"author is: " + mbook.getauthor() + "" +
"publishtime is: " + mbook.getpublishtime()); setcontentview(mtextview);
}
}
package com.test.tutor.objecttran;
import android.app.activity;
import android.os.bundle;
import android.widget.textview;
public class objecttrandemo2 extends activity {
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
textview mtextview = new textview(this);
book mbook = (book)getintent().getparcelableextra(objecttrandemo.par_key);
mtextview.settext("book name is: " + mbook.getbookname()+""+
"author is: " + mbook.getauthor() + "" +
"publishtime is: " + mbook.getpublishtime());
setcontentview(mtextview);
}
}
下面是最重要的环节:修改androidmanifest.xml文件(将两个新增的activity,objecttestdemo1,objecttestdemo2)申明一下代码如下(第14,15行):
代码
< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.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=".objecttestdemo1">< /activity>
< activity android:name=".objecttestdemo2">< /activity>
< /application>
< uses-sdk android:minsdkversion="7" />
< /manifest>
< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.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=".objecttestdemo1">< /activity>
< activity android:name=".objecttestdemo2">< /activity>
< /application>
< uses-sdk android:minsdkversion="7" />
< /manifest>