原文网址: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. OC - 7.Foundation框架的简单介绍

    OC语言-07-OC语言-Foundation框架   结构体 NSRange/CGRange 用来表示一个元素在另一个元素中的范围,NSRange等价于CGRange 包含两个属性: NSUInte ...

  2. Cesium的api之关于viewer(二)

    1.构建一个viewer,如下创建:options的参数根据实际情况,进行设定 var viewer = new Cesium.Viewer('cesiumContainer', { //Start ...

  3. Spring+Maven+Eclipse构建Web工程

    转载请注明出处:http://www.cnblogs.com/lidabnu/p/5657439.html 1 环境准备 下载Eclipse:http://www.eclipse.org/downlo ...

  4. UVA 10739 String to Palindrome(动态规划 回文)

    String to Palindrome 题目大意:给出一个字符串s,现在可以进行3种操作(添加字母,删除字母,替换字母),将其变成回文串,求出最少的操作次数.比如abccda,可以用删除操作,删除b ...

  5. cocos2dx如何添加popScene的场景动画

    说明 我们很容易在pushScene中添加动画 Director::getInstance()->pushScene(TransitionSlideInB::create(SCENE_TIME, ...

  6. c++ primer复习(二)

    1 悬垂else来自于else语句搭配的if语句的二义性 2 理解switch代码: switch(c) { case 'a': a++; case 'b': b++; default: x++; } ...

  7. mysql远程连接缓慢的问题

    这两天发现服务器程序启动的时候到了mysql初始连接的那一步很耗时,启动缓慢,后来发现,将连接的主机的-h参数改成localhost的时候 瞬间就完成连接了.后来在网上查到,原来是由于mysql服务器 ...

  8. Js获取fileupload的绝对路径时总是的到C:\fakepath\+文件名称的 解决方案

    解决方法: Internet选项->安全->自定义级别->将文件下载到服务器时包含本地目录路径  启用就可以了.

  9. ThinkPHP下使用Ueditor

    在做课程设计的时候想到用百度的Ueditor,可在配置的时候出现了一些问题 Ueditor感觉不是很难,以前有个人定制的,现在取消了这项服务,但是我们可以自己进行配置 下载地址:http://uedi ...

  10. sass中 混合宏 VS 继承 VS 占位符 各自的使用时机和特点

    初学者都常常纠结于这个问题“什么时候用混合宏,什么时候用继承,什么时候使用占位符?”其实他们各有各的优点与缺点,先来看看他们使用效果: a) Sass 中的混合宏使用 举例代码见 2-24 行 编译出 ...