Android系列之Intent传递对象的两种方法
2010-11-14 12:28 $等待$ 阅读(3261) 评论(0) 编辑 收藏 举报在Android中intent传递对象主要有2种方式分别是,Bundle.putSerializable(Key,Object)和Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,以下是我为大家做的一个实例
首先我们建立一个工程项目命名为:ObjectTestDemo
然后我们再修改main.xml布局文件,主要增加2个按钮
代码
1 view plaincopy to clipboardprint?
2
3 < ?xml version="1.0" encoding="utf-8"?>
4
5 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
6
7 android:orientation="vertical"
8
9 android:layout_width="fill_parent"
10
11 android:layout_height="fill_parent"
12
13 >
14
15 < TextView
16
17 android:layout_width="fill_parent"
18
19 android:layout_height="wrap_content"
20
21 android:text="Welcome to Mr Jesson's blog."
22
23 />
24
25 < Button
26
27 android:id="@+id/button1"
28
29 android:layout_width="fill_parent"
30
31 android:layout_height="wrap_content"
32
33 android:text="Serializable"
34
35 />
36
37 < Button
38
39 android:id="@+id/button2"
40
41 android:layout_width="fill_parent"
42
43 android:layout_height="wrap_content"
44
45 android:text="Parcelable"
46
47 />
48
49 < /LinearLayout>
50
51 < ?xml version="1.0" encoding="utf-8"?>
52
53 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
54
55 android:orientation="vertical"
56
57 android:layout_width="fill_parent"
58
59 android:layout_height="fill_parent"
60
61 >
62
63 < TextView
64
65 android:layout_width="fill_parent"android:layout_height="wrap_content"
66
67 android:text="Welcome to Mr jesson's blog."
68
69 />
70
71 < Button
72
73 android:id="@+id/button1"
74
75 android:layout_width="fill_parent"
76
77 android:layout_height="wrap_content"
78
79 android:text="Serializable"
80
81 />
82
83 < Button
84
85 android:id="@+id/button2"
86
87 android:layout_width="fill_parent"
88
89 android:layout_height="wrap_content"
90
91 android:text="Parcelable"
92
93 />
94
95 < /LinearLayout>
96
接下来我们开始对工程进行实现,分别建立Person.java实现Serializable接口,另一个Book.java实现Parcelable接口
代码
1 package com.test.objecttran;
2
3 import java.io.Serializable;
4
5 public class Person implements Serializable {
6
7 private static final long serialVersionUID = -7060210544600464481L;
8
9 private String name;
10
11 private int age;
12
13 public String getName() {
14
15 return name;
16
17 }
18
19 public void setName(String name) {
20
21 this.name = name;
22
23 }
24
25 public int getAge() {
26
27 return age;
28
29 }
30
31 public void setAge(int age) {
32
33 this.age = age;
34
35 }
36
37 }
38
代码
1 package com.test.tutor.objecttran;
2
3 import java.io.Serializable;
4
5 public class Person implements Serializable {
6
7 private static final long serialVersionUID = -7060210544600464481L;
8
9 private String name;
10
11 private int age;
12
13 public String getName() {
14
15 return name;
16
17 }
18
19 public void setName(String name) {
20
21 this.name = name;
22
23 }
24
25 public int getAge() {
26
27 return age;
28
29 }
30
31 public void setAge(int age) {
32
33 this.age = age;
34
35 }
36
37 }
38
代码
1 package com.test.tutor.objecttran;
2
3
4
5 import android.os.Parcel;
6
7 import android.os.Parcelable;
8
9 public class Book implements Parcelable {
10
11 private String bookName;
12
13 private String author;
14
15 private int publishTime;
16
17 public String getBookName() {
18
19 return bookName;
20
21 }
22
23 public void setBookName(String bookName) {
24
25 this.bookName = bookName;
26
27 }
28
29 public String getAuthor() {
30
31 return author;
32
33 }
34
35 public void setAuthor(String author) {
36
37 this.author = author;
38
39 }
40
41 public int getPublishTime() {
42
43 return publishTime;
44
45 }
46
47 public void setPublishTime(int publishTime) {
48
49 this.publishTime = publishTime;
50
51 }
52
53 public static final Parcelable.Creator CREATOR = new Creator() {
54
55 public Book createFromParcel(Parcel source) {
56
57 Book mBook = new Book();
58
59 mBook.bookName = source.readString();
60
61 mBook.author = source.readString();
62
63 mBook.publishTime = source.readInt();
64
65 return mBook;
66
67 }
68
69 public Book[] newArray(int size) {
70
71 return new Book[size];
72
73 }
74
75 };
76
77 public int describeContents() {
78
79 return 0;
80
81 }
82
83 public void writeToParcel(Parcel parcel, int flags) {
84
85 parcel.writeString(bookName);
86
87 parcel.writeString(author);
88
89 parcel.writeInt(publishTime);
90
91 }
92
93 }
94
代码
1 package com.test.tutor.objecttran;
2
3 import android.os.Parcel;
4
5 import android.os.Parcelable;
6
7 public class Book implements Parcelable {
8
9 private String bookName;
10
11 private String author;
12
13 private int publishTime;
14
15 public String getBookName() {
16
17 return bookName;
18
19 }
20
21 public void setBookName(String bookName) {this.bookName = bookName;
22
23 }
24
25 public String getAuthor() {
26
27 return author;
28
29 }
30
31 public void setAuthor(String author) {
32
33 this.author = author;
34
35 }
36
37 public int getPublishTime() {
38
39 return publishTime;
40
41 }
42
43 public void setPublishTime(int publishTime) {
44
45 this.publishTime = publishTime;
46
47 }
48
49 public static final Parcelable.Creator CREATOR = new Creator() {
50
51 public Book createFromParcel(Parcel source) {
52
53 Book mBook = new Book();
54
55 mBook.bookName = source.readString();
56
57 mBook.author = source.readString();
58
59 mBook.publishTime = source.readInt();
60
61 return mBook;
62
63 }
64
65 public Book[] newArray(int size) {
66
67 return new Book[size];
68
69 }
70
71 };
72
73 public int describeContents() {
74
75 return 0;
76
77 }
78
79 public void writeToParcel(Parcel parcel, int flags) {
80
81 parcel.writeString(bookName);
82
83 parcel.writeString(author);
84
85 parcel.writeInt(publishTime);
86
87 }
88
89 }
90
修改ObjectTranDemo.java,并且新建两个Activity,一个是ObjectTranDemo1.java,别一个是ObjectTranDemo2.java.分别用来显示Person对像数据,和Book对象数据
代码
1 package com.test.tutor.objecttran;
2
3 import android.app.Activity;
4
5 import android.content.Intent;
6
7 import android.os.Bundle;
8
9 import android.view.View;
10
11 import android.view.View.OnClickListener;
12
13 import android.widget.Button;
14
15 public class ObjectTranDemo extends Activity implements OnClickListener {
16
17 private Button sButton,pButton;
18
19 public final static String SER_KEY = "com.tutor.objecttran.ser";
20
21 public final static String PAR_KEY = "com.tutor.objecttran.par";
22
23 public void onCreate(Bundle savedInstanceState) {
24
25 super.onCreate(savedInstanceState);
26
27 setContentView(R.layout.main);
28
29 setupViews();
30
31 }
32
33
34 public void setupViews(){
35
36 sButton = (Button)findViewById(R.id.button1);
37
38 pButton = (Button)findViewById(R.id.button2);
39
40 sButton.setOnClickListener(this);
41
42 pButton.setOnClickListener(this);
43
44 }
45
46 //Serializeable传递对象的方法
47
48 public void SerializeMethod(){
49
50 Person mPerson = new Person();
51
52 mPerson.setName("frankie");
53
54 mPerson.setAge(25);
55
56 Intent mIntent = new Intent(this,ObjectTranDemo1.class);
57
58 Bundle mBundle = new Bundle();
59
60 mBundle.putSerializable(SER_KEY,mPerson);
61
62 mIntent.putExtras(mBundle);
63
64 startActivity(mIntent);
65
66 }
67
68 //Pacelable传递对象方法
69
70 public void PacelableMethod(){
71
72 Book mBook = new Book();
73
74 mBook.setBookName("Android Tutor");
75
76 mBook.setAuthor("Frankie");
77
78 mBook.setPublishTime(2010);
79
80 Intent mIntent = new Intent(this,ObjectTranDemo2.class);
81
82 Bundle mBundle = new Bundle();
83
84 mBundle.putParcelable(PAR_KEY, mBook);
85
86 mIntent.putExtras(mBundle);
87
88 startActivity(mIntent);
89
90 }
91
92 //铵钮点击事件响应
93
94 public void onClick(View v) {
95
96 if(v == sButton){
97
98 SerializeMethod();
99
100 }else{
101
102 PacelableMethod();
103
104 }
105
106 }
107
108 }
109
代码
1 package com.test.tutor.objecttran;
2
3 import android.app.Activity;
4
5 import android.content.Intent;
6
7 import android.os.Bundle;
8
9 import android.view.View;
10
11 import android.view.View.OnClickListener;
12
13 import android.widget.Button;
14
15 public class ObjectTranDemo extends Activity implements OnClickListener {
16
17
18
19 private Button sButton,pButton;
20
21 public final static String SER_KEY = "com.tutor.objecttran.ser";
22
23 public final static String PAR_KEY = "com.tutor.objecttran.par";
24
25 public void onCreate(Bundle savedInstanceState) {
26
27 super.onCreate(savedInstanceState);
28
29 setContentView(R.layout.main);
30
31 setupViews();
32
33 }
34
35 public void setupViews(){
36
37 sButton = (Button)findViewById(R.id.button1);
38
39 pButton = (Button)findViewById(R.id.button2);
40
41 sButton.setOnClickListener(this);
42
43 pButton.setOnClickListener(this);
44
45 }
46
47 //Serializeable传递对象的方法
48
49 public void SerializeMethod(){
50
51 Person mPerson = new Person();
52
53 mPerson.setName("frankie");
54
55 mPerson.setAge(25);
56
57 Intent mIntent = new Intent(this,ObjectTranDemo1.class);
58
59 Bundle mBundle = new Bundle();
60
61 mBundle.putSerializable(SER_KEY,mPerson);
62
63 mIntent.putExtras(mBundle);
64
65 startActivity(mIntent);
66
67 }
68
69 //Pacelable传递对象方法
70
71 public void PacelableMethod(){
72
73 Book mBook = new Book();
74
75 mBook.setBookName("Android Tutor");
76
77 mBook.setAuthor("Frankie");
78
79 mBook.setPublishTime(2010);
80
81 Intent mIntent = new Intent(this,ObjectTranDemo2.class);
82
83 Bundle mBundle = new Bundle();
84
85 mBundle.putParcelable(PAR_KEY, mBook);
86
87 mIntent.putExtras(mBundle);
88
89 startActivity(mIntent);
90
91 }
92
93 //铵钮点击事件响应
94
95 public void onClick(View v) {
96
97 if(v == sButton){
98
99 SerializeMethod();
100
101 }else{
102
103 PacelableMethod();
104
105 }
106
107 }
108
109 }
110
代码
1 package com.test.tutor.objecttran;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.widget.TextView;
6
7 public class ObjectTranDemo1 extends Activity {
8
9 @Override
10
11 public void onCreate(Bundle savedInstanceState) {
12
13 super.onCreate(savedInstanceState);
14
15 TextView mTextView = new TextView(this);
16
17 Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);
18
19 mTextView.setText("You name is: " + mPerson.getName() + ""+
20
21 "You age is: " + mPerson.getAge());
22
23 setContentView(mTextView);
24
25 }
26
27 }
28
代码
1 package com.test.tutor.objecttran;
2
3 import android.app.Activity;
4
5 import android.os.Bundle;
6
7 import android.widget.TextView;
8
9 public class ObjectTranDemo1 extends Activity {
10
11 @Override
12
13 public void onCreate(Bundle savedInstanceState) {
14
15 super.onCreate(savedInstanceState);
16
17 TextView mTextView = new TextView(this);
18
19 Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);
20
21 mTextView.setText("You name is: " + mPerson.getName() + ""+
22
23 "You age is: " + mPerson.getAge());
24
25 setContentView(mTextView);
26
27 }
28
29 }
30
代码
1 package com.test.tutor.objecttran;
2
3 import android.app.Activity;
4
5 import android.os.Bundle;
6
7 import android.widget.TextView;
8
9 public class ObjectTranDemo2 extends Activity {
10
11 public void onCreate(Bundle savedInstanceState) {
12
13 super.onCreate(savedInstanceState);
14
15 TextView mTextView = new TextView(this);
16
17 Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);
18
19 mTextView.setText("Book name is: " + mBook.getBookName()+""+
20
21 "Author is: " + mBook.getAuthor() + "" +
22
23 "PublishTime is: " + mBook.getPublishTime()); setContentView(mTextView);
24
25 }
26
27 }
28
代码
1 package com.test.tutor.objecttran;
2
3 import android.app.Activity;
4
5 import android.os.Bundle;
6
7 import android.widget.TextView;
8
9 public class ObjectTranDemo2 extends Activity {
10
11 public void onCreate(Bundle savedInstanceState) {
12
13 super.onCreate(savedInstanceState);
14
15 TextView mTextView = new TextView(this);
16
17 Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);
18
19 mTextView.setText("Book name is: " + mBook.getBookName()+""+
20
21 "Author is: " + mBook.getAuthor() + "" +
22
23 "PublishTime is: " + mBook.getPublishTime());
24
25 setContentView(mTextView);
26
27 }
28
29 }
30
下面是最重要的环节:修改AndroidManifest.xml文件(将两个新增的Activity,ObjecttestDemo1,ObjecttestDemo2)申明一下代码如下(第14,15行):
代码
1 < ?xml version="1.0" encoding="utf-8"?>
2
3 < manifest xmlns:android="http://schemas.android.com/apk/res/android"
4
5 package="com.test.tutor.objecttran"
6
7 android:versionCode="1"
8
9 android:versionName="1.0">
10
11 < application android:icon="@drawable/icon" android:label="@string/app_name">
12
13 < activity android:name=".ObjectTranDemo"
14
15 android:label="@string/app_name">
16
17 < intent-filter>
18
19 < action android:name="android.intent.action.MAIN" />
20
21 < category android:name="android.intent.category.LAUNCHER" />
22
23 < /intent-filter>
24
25 < /activity>
26
27 < activity android:name=".ObjecttestDemo1">< /activity>
28
29 < activity android:name=".ObjecttestDemo2">< /activity>
30
31 < /application>
32
33 < uses-sdk android:minSdkVersion="7" />
34
35 < /manifest>
36
37 < ?xml version="1.0" encoding="utf-8"?>
38
39 < manifest xmlns:android="http://schemas.android.com/apk/res/android"
40
41 package="com.test.tutor.objecttran"
42
43 android:versionCode="1"
44
45 android:versionName="1.0">
46
47 < application android:icon="@drawable/icon" android:label="@string/app_name">
48
49 < activity android:name=".ObjectTranDemo"
50
51 android:label="@string/app_name">
52
53 < intent-filter>
54
55 < action android:name="android.intent.action.MAIN" />
56
57 < category android:name="android.intent.category.LAUNCHER" />
58
59 < /intent-filter>
60
61 < /activity>
62
63 < activity android:name=".ObjecttestDemo1">< /activity>
64
65 < activity android:name=".ObjecttestDemo2">< /activity>
66
67 < /application>
68
69 < uses-sdk android:minSdkVersion="7" />
70
71 < /manifest>
72
再接下来就是编译,运行看效果了哈!!!