原文网址: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. ASP.NET MVC5总结(一)@HTML和对应的HTML

    HtmlHelper用来在视图中呈现 HTML 控件,主要分为以下几类: 1.ActionLink - 链接到操作方法 @Html.ActionLink("这是一个连接", &qu ...

  2. asp.net:用类来后台绑定数据源

    //封装成一个 using System;using System.Collections.Generic;using System.Linq;using System.Web;using Syste ...

  3. java新手笔记1 Hello World!

    //Hello.java文件 //类声明 public class Hello{ //声明方法 main程序入口 public static void main (String[] args) { S ...

  4. Ambiguous handler methods mapped for HTTP path

    一.问题:映射重复导致的错误 java代码如下: @RequestMapping(value = "/info/{remove}/{id}", method = RequestMe ...

  5. Codevs 5208 求乘方取模

    5208 求乘方取模 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 未定级 题目描述 Description 给定非负整数A.B.M,求(A ^ B) mod M. 输入描述 Inpu ...

  6. HttpUtility.HtmlEncode

    HttpUtility.HtmlEncode用来防止站点受到恶意脚本注入的攻击 public string Welcome(string name, int numTimes = 1) {     r ...

  7. 安装hadoop多节点 各种整理

    ubuntu烧制usb启动盘链接: 点击打开链接https://help.ubuntu.com/community/Installation/FromUSBStick ubuntu磁盘分区: 点击打开 ...

  8. IE6 png 透明 (三种解决方法)

    FF和IE7已经直接支持透明的png图了,下面这个主要是解决IE6下透明PNG图片有灰底的 ====================================================== ...

  9. session 保存在指定的数据表,cookie设置

    首先建立数据表,可在ThinkPHP/Extend/Driver/Session/SessionDb.class.php中copy代码 在配置文件中配置: 'SESSION_TYPE' => ' ...

  10. java中运算符——进度1

    Class Demo1{    public static void main(String[] args) {        /*        一.逻辑运算法用于连接两个boolean类型的表达式 ...