Android系列之Intent传递对象的两种方法

原文地址:http://www.apkbus.com/forum.php?mod=viewthread&tid=914&highlight=Android%E7%B3%BB%E5%88%97%E4%B9%8B 

Androidintent传递对象主要有2种方式分别是,Bundle.putSerializable(Key,Object)和Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,以下是我为大家做的一个实例。

       首先我们建立一个工程项目命名为:ObjectTestDemo。然后我们再修改main.xml布局文件,主要增加2个按钮。

Java代码:

  1. < ?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:orientation="vertical"

  4. android:layout_width="fill_parent"

  5. android:layout_height="fill_parent"

  6. >

  7. < TextView

  8. android:layout_width="fill_parent"

  9. android:layout_height="wrap_content"

  10. android:text="Welcome to Mr Jesson's blog."

  11. />

  12. < Button

  13. android:id="@+id/button1"

  14. android:layout_width="fill_parent"

  15. android:layout_height="wrap_content"

  16. android:text="Serializable"

  17. />

  18. < Button

  19. android:id="@+id/button2"

  20. android:layout_width="fill_parent"

  21. android:layout_height="wrap_content"

  22. android:text="Parcelable"

  23. />

  24. < /LinearLayout>
复制代码
接下来我们开始对工程进行实现,分别建立 Person.java实现 Serializable接口,另一个 Book.java实现 Parcelable接口:

Java代码:

  1. package eoe.test.objecttran;

  2. import java.io.Serializable;

  3. public class Person implements Serializable {

  4. private static final long serialVersionUID = -7060210544600464481L;

  5. private String name;

  6. private int age;

  7. public String getName() {

  8. return name;

  9. }

  10. public void setName(String name) {

  11. this.name = name;

  12. }

  13. public int getAge() {

  14. return age;

  15. }

  16. public void setAge(int age) {

  17. this.age = age;

  18. }

  19. }
复制代码
Java代码:

  1. package eoe.test.tutor.objecttran;


  2. import android.os.Parcel;
  3. import android.os.Parcelable;

  4. public class Book implements Parcelable {

  5. private String bookName;

  6. private String author;

  7. private int publishTime;

  8. public String getBookName() {

  9. return bookName;

  10. }

  11. public void setBookName(String bookName) {

  12. this.bookName = bookName;

  13. }

  14. public String getAuthor() {

  15. return author;

  16. }

  17. public void setAuthor(String author) {

  18. this.author = author;

  19. }

  20. public int getPublishTime() {

  21. return publishTime;

  22. }

  23. public void setPublishTime(int publishTime) {

  24. this.publishTime = publishTime;

  25. }

  26. public static final Parcelable.Creator CREATOR = new Creator() {

  27. public Book createFromParcel(Parcel source) {

  28. Book mBook = new Book();

  29. mBook.bookName = source.readString();

  30. mBook.author = source.readString();

  31. mBook.publishTime = source.readInt();

  32. return mBook;

  33. }

  34. public Book[] newArray(int size) {

  35. return new Book[size];

  36. }

  37. };

  38. public int describeContents() {

  39. return 0;

  40. }

  41. public void writeToParcel(Parcel parcel, int flags) {

  42. parcel.writeString(bookName);

  43. parcel.writeString(author);

  44. parcel.writeInt(publishTime);

  45. }

  46. }
复制代码

   修改 ObjectTranDemo.java,并且新建两个 Activity,一个是 ObjectTranDemo1.java,别一个是 ObjectTranDemo2.java.分别用来显示 Person对像数据,和 Book对象数据.

Java代码:

  1. package eoe.test.tutor.objecttran;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;

  8. public class ObjectTranDemo extends Activity implements OnClickListener {

  9. private Button sButton,pButton;

  10. public final static String SER_KEY = "com.tutor.objecttran.ser";

  11. public final static String PAR_KEY = "com.tutor.objecttran.par";

  12. public void onCreate(Bundle savedInstanceState) {

  13. super.onCreate(savedInstanceState);

  14. setContentView(R.layout.main);

  15. setupViews();

  16. }


  17. public void setupViews(){

  18. sButton = (Button)findViewById(R.id.button1);

  19. pButton = (Button)findViewById(R.id.button2);

  20. sButton.setOnClickListener(this);

  21. pButton.setOnClickListener(this);

  22. }

  23. //Serializeable传递对象的方法

  24. public void SerializeMethod(){

  25. Person mPerson = new Person();

  26. mPerson.setName("frankie");

  27. mPerson.setAge(25);

  28. Intent mIntent = new Intent(this,ObjectTranDemo1.class);

  29. Bundle mBundle = new Bundle();

  30. mBundle.putSerializable(SER_KEY,mPerson);

  31. mIntent.putExtras(mBundle);

  32. startActivity(mIntent);

  33. }

  34. //Pacelable传递对象方法

  35. public void PacelableMethod(){

  36. Book mBook = new Book();

  37. mBook.setBookName("Android Tutor");

  38. mBook.setAuthor("Frankie");

  39. mBook.setPublishTime(2010);

  40. Intent mIntent = new Intent(this,ObjectTranDemo2.class);

  41. Bundle mBundle = new Bundle();

  42. mBundle.putParcelable(PAR_KEY, mBook);

  43. mIntent.putExtras(mBundle);

  44. startActivity(mIntent);

  45. }

  46. //铵钮点击事件响应

  47. public void onClick(View v) {

  48. if(v == sButton){

  49. SerializeMethod();

  50. }else{

  51. PacelableMethod();

  52. }

  53. }
  54. }
复制代码

Java代码:

  1. package com.test.tutor.objecttran;

  2. import android.app.Activity;
  3. import android.os.Bundle; 
  4. import android.widget.TextView;

  5. public class ObjectTranDemo1 extends Activity {

  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {

  8. super.onCreate(savedInstanceState);

  9. TextView mTextView = new TextView(this);

  10. Person m Person= (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);

  11. mTextView.setText("You name is: " + mPerson.getName() + ""+
  12. "You age is: " + mPerson.getAge());

  13. setContentView(mTextView);

  14. }

  15. }
复制代码
Java代码:

  1. package com.test.tutor.objecttran;

  2. import android.app.Activity;
  3. import android.os.Bundle;

  4. import android.widget.TextView;

  5. public class ObjectTranDemo2 extends Activity {

  6. public void onCreate(Bundle savedInstanceState) {

  7. super.onCreate(savedInstanceState);

  8. TextView mTextView = new TextView(this);

  9. Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);

  10. mTextView.setText("Book name is: " + mBook.getBookName()+""+
  11. "Author is: " + mBook.getAuthor() + "" +
  12. "PublishTime is: " + mBook.getPublishTime()); setContentView(mTextView);

  13. }

  14. }
复制代码
    下面是最重要的环节:修改 AndroidManifest.xml文件(将两个新增的 ActivityObjecttestDemo1,ObjecttestDemo2)申明一下代码如下(第14,15行):

Java代码:

  1. < ?xml version="1.0" encoding="utf-8"?>
  2. < manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. package="eoe.test.tutor.objecttran"

  4. android:versionCode="1"

  5. android:versionName="1.0">

  6. < application android:icon="@drawable/icon" android:label="@string/app_name">

  7. < activity android:name=".ObjectTranDemo"

  8. android:label="@string/app_name">

  9. < intent-filter>

  10. < action android:name="android.intent.action.MAIN" />

  11. < category android:name="android.intent.category.LAUNCHER" />

  12. < /intent-filter>

  13. < /activity>

  14. < activity android:name=".ObjecttestDemo1">< /activity>

  15. < activity android:name=".ObjecttestDemo2">< /activity>

  16. < /application>

  17. < uses-sdk android:minSdkVersion="7" />

  18. < /manifest>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值