Android中使用Intent在Activity之间传递对象(使用Serializable或者Parcelable)的方法

时间:2022-12-03 11:24:29

 android中的不同activity之间传递对象,我们可以考虑采用bundle.putserializable(key,object);也可以考虑采用bundle.putparcelable(key, object);其中前面一种方法中的object要实现serializable接口,后面一种方法中的object要实现parcelable接口。下面我们以一个完整的例子来说明。

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

Android中使用Intent在Activity之间传递对象(使用Serializable或者Parcelable)的方法

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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="@string/hello"
/>
<button
android:id="@+id/serbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="serializable"/>
<button
android:id="@+id/parbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="parcelable"/>
</linearlayout>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
person.java:
package com.andy.entity;
import java.io.serializable;
public class person implements serializable {
private static final long serialversionuid = -6919461967497580385l;
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;
}
}
police.java:
[c-sharp] view plain copy
package com.andy.entity;
import android.os.parcel;
import android.os.parcelable;
public class police implements parcelable {
private string name;
private int worktime;
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public int getworktime() {
return worktime;
}
public void setworktime(int worktime) {
this.worktime = worktime;
}
public static final parcelable.creator<police> creator = new creator<police>() {
@override
public police createfromparcel(parcel source) {
police police = new police();
police.name = source.readstring();
police.worktime = source.readint();
return police;
}
@override
public police[] newarray(int size) {
return new police[size];
}
};
@override
public int describecontents() {
return 0;
}
@override
public void writetoparcel(parcel parcel, int flags) {
parcel.writestring(name);
parcel.writeint(worktime);
}
}

4.在包com.andy.testdemo中修改testactivity.java类,同时在该包中添加类serializabledemo和parcelabledemo,分别继承了activity类和分别显示person对象和police对象的数据。代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.andy.testdemo;
import com.andy.entity.person;
import com.andy.entity.police;
import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.view.view;
import android.widget.button;
public class testactivity extends activity {
private button sbutton,pbutton;
public final static string ser_key = "com.andy.ser";
public final static string par_key = "com.andy.par";
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
sbutton = (button)findviewbyid(r.id.serbutton);
sbutton.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
serializemethod();
}
});
pbutton = (button)findviewbyid(r.id.parbutton);
pbutton.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
pacelablemethod();
}
});
}
/**
* serializeable传递对象的方法
*/
private void serializemethod(){
person mperson = new person();
mperson.setname("andy");
mperson.setage(26);
intent mintent = new intent(this,serializabledemo.class);
bundle mbundle = new bundle();
mbundle.putserializable(ser_key,mperson);
mintent.putextras(mbundle);
startactivity(mintent);
}
/**
* pacelable传递对象方法
*/
private void pacelablemethod(){
police mpolice = new police();
mpolice.setname("i am police");
mpolice.setworktime(2008);
intent mintent = new intent(this,parcelabledemo.class);
bundle mbundle = new bundle();
mbundle.putparcelable(par_key, mpolice);
mintent.putextras(mbundle);
startactivity(mintent);
}
}

serializabledemo.java类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.andy.testdemo;
import com.andy.entity.person;
import android.app.activity;
import android.os.bundle;
import android.widget.textview;
public class serializabledemo extends activity {
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
textview mtextview = new textview(this);
person mperson = (person)getintent().getserializableextra(testactivity.ser_key);
mtextview.settext("you name is: " + mperson.getname() + "/n"+
"you age is: " + mperson.getage());
setcontentview(mtextview);
}
}

parcelabledemo.java类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.andy.testdemo;
import com.andy.entity.police;
import android.app.activity;
import android.os.bundle;
import android.widget.textview;
public class parcelabledemo extends activity {
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
textview mtextview = new textview(this);
police mpolice = (police)getintent().getparcelableextra(testactivity.par_key);
mtextview.settext("police name is: " + mpolice.getname()+"/n"+
"worktime is: " + mpolice.getworktime() + "/n");
setcontentview(mtextview);
}
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andy.testdemo"
android:versioncode="1"
android:versionname="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".testactivity"
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=".serializabledemo"/>
<activity android:name=".parcelabledemo"/>
</application>
<uses-sdk android:minsdkversion="8" />
</manifest>

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

【1】主界面截图:

Android中使用Intent在Activity之间传递对象(使用Serializable或者Parcelable)的方法

【2】点击serializable按钮的效果

Android中使用Intent在Activity之间传递对象(使用Serializable或者Parcelable)的方法

【3】点击parcelable按钮的效果

Android中使用Intent在Activity之间传递对象(使用Serializable或者Parcelable)的方法

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