原文网址:http://blog.csdn.net/lixiang0522/article/details/8642202

android中的组件间传递的对象一般实现Parcelable接口,当然也可以使用java的Serializable接口,前者是android专门设计的,效率更高,下面我们就来实现一个Parcelabel。
 
1. 创建一个类实现Parcelable接口,具体实现如下:
  1. package com.hebaijun.testparcelable;
  2. import android.os.Parcel;
  3. import android.os.Parcelable;
  4. public class ParcelableData implements Parcelable{
  5. private String name;
  6. private int age;
  7. public ParcelableData(){
  8. name = "guest";
  9. age = 20;
  10. }
  11. public ParcelableData(Parcel in){
  12. //顺序要和writeToParcel写的顺序一样
  13. name = in.readString();
  14. age = in.readInt();
  15. }
  16. public String getName(){
  17. return name;
  18. }
  19. public void setName(String name){
  20. this.name = name;
  21. }
  22. public int getAge(){
  23. return age;
  24. }
  25. public void setAge(int age) {
  26. this.age = age;
  27. }
  28. @Override
  29. public int describeContents() {
  30. // TODO Auto-generated method stub
  31. return 0;
  32. }
  33. @Override
  34. public void writeToParcel(Parcel dest, int flags) {
  35. // TODO Auto-generated method stub
  36. dest.writeString(name);
  37. dest.writeInt(age);
  38. }
  39. public static final Parcelable.Creator<ParcelableData> CREATOR = new Parcelable.Creator<ParcelableData>() {
  40. public ParcelableData createFromParcel(Parcel in) {
  41. return new ParcelableData(in);
  42. }
  43. public ParcelableData[] newArray(int size) {
  44. return new ParcelableData[size];
  45. }
  46. };
  47. }
2. 通过下面的方法发送对象。Bundle类也实现了Parcelable接口,一般在android中我们是通过Bundle来封装数据并进行传送的。
  1. Intent intent = new Intent();
  2. intent.setClass(this, SubActivity.class);
  3. // 直接添加
  4. //intent.putExtra("MyData", new ParcelableData());
  5. // 通过Bundle
  6. Bundle bundle = new Bundle();
  7. bundle.putString("MyString", "test bundle");
  8. bundle.putParcelable("MyData", new ParcelableData());
  9. intent.putExtras(bundle);
  10. startActivity(intent);
3. 下面的接收对象的方法。
  1. //ParcelableData parcelableData = getIntent().getParcelableExtra("MyData");
  2. Bundle bundle = getIntent().getExtras();
  3. ParcelableData parcelableData = bundle.getParcelable("MyData");
  4. String testBundleString = bundle.getString("MyString");
  5. Log.v("string=", testBundleString);
  6. Log.v("name=", parcelableData.getName());
  7. Log.v("age=", ""+parcelableData.getAge());

传输的对象需要实现序列化:有两种方式,一种是实现Serializable接口,就是原来的java方式;另外一种是android的Parcelable方式,这个性能可能好一些,我猜的,但是这在需要手动去写Parcelable接口的实现。

Serializable存数据:

  1. Person mPerson = new Person();
  2. mPerson.setName("frankie");
  3. mPerson.setAge(25);
  4. Intent mIntent = new Intent(this,ObjectTranDemo1.class);
  5. Bundle mBundle = new Bundle();
  6. mBundle.putSerializable(SER_KEY,mPerson);
  7. mIntent.putExtras(mBundle);

Serializable取数据:

 // 获取启动该ResultActivity的Intent
24         Intent intent = getIntent();
25         // 获取该Intent所携带的数据
26         Bundle bundle = intent.getExtras();
27         // 从bundle数据包中取出数据
28         Person person = (Person) bundle.getSerializable("person");

Parcelable存数据:

  1. Intent mIntent = new Intent(this,ObjectTranDemo2.class);
  2. Bundle mBundle = new Bundle();
  3. mBundle.putParcelable(PAR_KEY, mBook);
  4. mIntent.putExtras(mBundle);

Parcelable取数据:

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

参考1:http://blog.csdn.net/Android_Tutor/article/details/5740845

参考2:http://my.oschina.net/u/577632/blog/76906

【转】Android中intent传递对象和Bundle的用法的更多相关文章

  1. Android中Intent传递对象的两种方法(Serializable,Parcelable)

    今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...

  2. [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)

    http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...

  3. Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!

    [转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...

  4. android#使用Intent传递对象

    参考自<第一行代码>——郭霖 Intent的用法相信你已经比较熟悉了,我们可以借助它来启动活动.发送广播.启动服务等.在进行上述操作的时候,我们还可以在Intent中添加一些附加数据,以达 ...

  5. Android中Intent传递类对象的方法一(Serializable)

    Activity之间通过Intent传递值,支持基本数据类型和String对象及它们的数组对象byte.byte[].char.char[].boolean.boolean[].short.short ...

  6. Android通过Intent传递对象

    1.传递Serializable方式类对象 首先创建一个序列化类:User import java.io.Serializable; public class User implements Seri ...

  7. Intent传递对象的几种方式

    原创文章.转载请注明 http://blog.csdn.net/leejizhou/article/details/51105060 李济洲的博客 Intent的使用方法相信你已经比較熟悉了,Inte ...

  8. Android 通过 Intent 传递类对象或list对象

    (转:http://www.cnblogs.com/shaocm/archive/2013/01/08/2851248.html) Android中Intent传递类对象提供了两种方式一种是 通过实现 ...

  9. Android 通过 Intent 传递类对象

    Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...

随机推荐

  1. 经历:easyui的datagrid没有数据滚动条的显示

    今天,一个用户提出一个这样的问题,"查询不到结果时,为什么我看不到后面的标题呢?" 最初,我听到这个问题时,第一反应是:查出来数据不就有滚动条了吗,干嘛非要较真呢? 不过,后来想想 ...

  2. ios UIWebView截获html并修改便签内容

    需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务器端 ...

  3. 控制器的跳转-modal与push

    一.modal与pushmodal从下面往上盖住原来的控制器,一般上一个控制器和下一个控制器没有什么关联时用modal,比如联系人的加号跳转页面,任何控制器都可以用modal push一般是上下文有关 ...

  4. UVA 10795 A Different Task(汉诺塔 递归))

    A Different Task The (Three peg) Tower of Hanoi problem is a popular one in computer science. Briefl ...

  5. VIM 及正则表达式

    VIM及正则表达式 一.查找/Search + 统计 1.统计某个关键字 方法是:%s:keyword:&:gn. 其中,keyword是要搜索的关键字,&表示前面匹配的字符串,n表示 ...

  6. PAT_1026 程序运行时间

    问题描述: 要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock ti ...

  7. MATLAB【工具箱下载】汇总

    至于工具箱的安装说明参见:http://www.matlabsky.com/thread-120-1-1.html Maplesoft<Maple Toolbox for MATLAB>  ...

  8. 【ADO.NET】5、手机归属地查询( winfrom )

    using System.IO; 有一个数据库手机号码的txt文件,格式是 : 13500000000-13560000000-中国移动 查询结果: 湖南移动[邵阳]文件夹选择对话框 FolderBr ...

  9. js 中如何通过提示框跳转页面

    通过提示框跳转页面 <!doctype html> <html lang="en"> <head> <meta charset=" ...

  10. php异步请求(可以做伪线程)

    $fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30); stream_set_blocking($fp,0);     ...