1. public class Intent implements Parcelable, Cloneable {   //... private String mAction;
  2. private Uri mData;
  3. private String mType;
  4. private String mPackage;
  5. private ComponentName mComponent;
  6. private int mFlags;
  7. private HashSet<String> mCategories;
  8. private Bundle mExtras;
  9. private Rect mSourceBounds;

转自:http://blog.csdn.net/annkie/article/details/8483253

Intent也是继承了Parcelable的接口。

个人理解,Intent应该只是一个数据参数的载体,真正将两个Acitivity/Service通信起来的是Binder接口(C/S架构)。

第一类:简单或基本数据类型

  1. Intent putExtra(String name, int[] value)
  2. Intent putExtra(String name, float value)
  3. Intent putExtra(String name, byte[] value)
  4. Intent putExtra(String name, long[] value)
  5. Intent putExtra(String name, float[] value)
  6. Intent putExtra(String name, long value)
  7. Intent putExtra(String name, String[] value)
  8. Intent putExtra(String name, boolean value)
  9. Intent putExtra(String name, boolean[] value)
  10. Intent putExtra(String name, short value)
  11. Intent putExtra(String name, double value)
  12. Intent putExtra(String name, short[] value)
  13. Intent putExtra(String name, String value)
  14. Intent putExtra(String name, byte value)
  15. Intent putExtra(String name, char[] value)
  16. Intent putExtra(String name, CharSequence[] value)

本质上仍然是通过一个Bundle(private Bundle mExtras;)来实现:

  1. public Intent putExtra(String name, long value) {
  2. if (mExtras == null) {
  3. mExtras = new Bundle();
  4. }
  5. mExtras.putLong(name, value);
  6. return this;
  7. }

第二类:传递一个Bundle

  1. public Intent putExtra(String name, Bundle value) {
  2. if (mExtras == null) {
  3. mExtras = new Bundle();
  4. }
  5. mExtras.putBundle(name, value);
  6. return this;
  7. }

第三类:传递Serializable对象

  1. public Intent putExtra(String name, Serializable value) {
  2. if (mExtras == null) {
  3. mExtras = new Bundle();
  4. }
  5. mExtras.putSerializable(name, value);
  6. return this;
  7. }

第四类:Parcelable对象

  1. public Intent putExtra(String name, Parcelable value) {
  2. if (mExtras == null) {
  3. mExtras = new Bundle();
  4. }
  5. mExtras.putParcelable(name, value);
  6. return this;
  7. }
  8. public Intent putExtra(String name, Parcelable[] value) {
  9. if (mExtras == null) {
  10. mExtras = new Bundle();
  11. }
  12. mExtras.putParcelableArray(name, value);
  13. return this;
  14. }

第五类:Intent

  1. public Intent putExtras(Intent src) {
  2. if (src.mExtras != null) {
  3. if (mExtras == null) {
  4. mExtras = new Bundle(src.mExtras);
  5. } else {
  6. mExtras.putAll(src.mExtras);
  7. }
  8. }
  9. return this;
  10. }

归根结底都是通过Bundle来实现数据封装。而Bundle则是通过Map的数据结构来存储数据。

mMap = new HashMap<String, Object>();

mParcelledData

两者同时只有一个有效。

一旦unparcel以后,mParcelledData

的数据将被填充到mMap中,同时值为null。在writeToParcel和readFromParcel中则直接使用mParcelledData.此时一般通过IBinder关联两个进程的通信。

关于Bundle则是实现了Parcelable接口的类,通过上面提到的HashMap和一个Parcel来存储数据。

  1. public final class Bundle implements Parcelable, Cloneable {
  2. private static final String LOG_TAG = "Bundle";
  3. public static final Bundle EMPTY;
  4. static {
  5. EMPTY = new Bundle();
  6. EMPTY.mMap = Collections.unmodifiableMap(new HashMap<String, Object>());
  7. }
  8. // Invariant - exactly one of mMap / mParcelledData will be null
  9. // (except inside a call to unparcel)
  10. /* package */ Map<String, Object> mMap = null;
  11. /*
  12. * If mParcelledData is non-null, then mMap will be null and the
  13. * data are stored as a Parcel containing a Bundle.  When the data
  14. * are unparcelled, mParcelledData willbe set to null.
  15. */
  16. /* package */ Parcel mParcelledData = null;
    1. public void putFloat(String key, float value) {
    2. unparcel();//首先解析出mParcelledData到mMap
    3. mMap.put(key, value);
    4. }
    5. /* package */ synchronized void unparcel() {
    6. if (mParcelledData == null) {
    7. return;
    8. }
    9. int N = mParcelledData.readInt();
    10. if (N < 0) {
    11. return;
    12. }
    13. if (mMap == null) {
    14. mMap = new HashMap<String, Object>();
    15. }
    16. mParcelledData.readMapInternal(mMap, N, mClassLoader);
    17. mParcelledData.recycle();
    18. mParcelledData = null;//回收以后值为null
    19. }

Android:Intent传递数据的几种类型和源码实现的更多相关文章

  1. Android Intent传递数据

    刚开始看郭大神的<>,实现以下里面的一些例子.Intent传递数据. 我们利用显示的方式进行Intent的启动. 1.启动intent并输入数据. Intent intent=new In ...

  2. Android Intent 传递数据注意事项

    不要通过 Intent 在 Android 基础组件之间传递大数据(binder transaction缓存为 1MB),可能导致 OOM.

  3. 【转】Android 之最新最全的Intent传递数据方法

    原文地址:https://www.jianshu.com/p/1169dba99261 intent传递数据 为什么要和intent单独拿出来讲,因为Intent传递数据也是非常重要的 一.简单的传递 ...

  4. Android学习笔记(十二)——使用意图传递数据的几种方式

    使用意图传递数据的几种方式 点此获取完整代码 我们除了要从活动返回数据,也经常要传递数据给活动.对此我们能够使用Intent对象将这些数据传递给目标活动. 1.创建一个名为PassingData的项目 ...

  5. Android 开发中使用Intent传递数据的方法

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

  6. Android学习之Intent传递数据

    Intent在Activity中的作用主要是有两个: 1.启动目标Activity 2.传递数据 Intent在传递数据时分两种情况:向下一个Activity传递数据和从下一个Activity返回数据 ...

  7. Android Activity传递数据使用getIntent()接收不到,揭秘Intent传递数据与Activity启动模式singleTask的关系。

    activity通过intent传递数据的时候,如果activity未启动,那么在这个刚启动的activity里通过getIntent()会获取到这个intent的数据.. 如果要启动的activit ...

  8. 安卓通过putExtra传递数据的几种方式

    通过intent传递数据时,使用以下代码报错: hMap<string, object=""> map=(Map<string, object="&qu ...

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

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

随机推荐

  1. 微信公众平台自定义菜单及高级接口PHP SDK

    本文介绍介绍微信公众平台自定义菜单及高级接口的PHP SDK及使用方法. 作者 方倍工作室 修正记录: 2014.05.03 v1.0 方倍工作室 http://www.cnblogs.com/txw ...

  2. EncodingAESKey

    关键字:EncodingAESKey 公众平台消息体签名及加解密方案概述 1.新增消息体签名验证,用于公众平台和公众账号验证消息体的正确性 2.针对推送给微信公众账号的普通消息和事件消息,以及推送给设 ...

  3. Android Error:warning: Ignoring InnerClasses attribute for an anonymous inner class

    今天项目发布时遇到了这个问题,在低版本设备上面死活发布不上去,还有打包也打不成功,折腾了好长一段时间,网上大部分给出的 解决方案都是说 在工程的混淆配置文件 proguard-rules.pro 中加 ...

  4. centOS中wget的使用方法

    对于 Linux 用户来说,几乎每天都在使用它. 下面为大家介绍几个有用的 CentOS wget 小技巧,可以让你更加高效而灵活的使用CentOS wget. CentOS wget 使用技巧 $ ...

  5. iOS多线程知识总结--GCD

    iOS多线程知识总结--GCD 1. iOS中苹果提供4钟方案来帮助我们实现多线程: (1) 纯C语言的pthread,偏底层,需要程序员手动管理线程的生命周期,基本不用. (2) OC语言的NSTr ...

  6. javascript:void(0)和javascript:;的用法

    一.JavaScript:void(0) 我们经常会使用到 javascript:void(0) 这样的代码,那么在 JavaScript 中 javascript:void(0) 代表的是什么意思呢 ...

  7. freemark 判断是否为空 是否存在

    <!--1\---><!--判断aaa是否不为空,eclipse插件老报错.---><#if aaa??>  ${aaa}</#if><!--2\ ...

  8. windows远程控制ubuntu尝试--未成功

    按照百度知道上的步骤一步一步操作,下载xrdp,一切很顺利. 直至出现了,如下的語言: connecting to sesman ip 尝试找到原因第一个原因 第二次的分析解释 至今还没找出原因

  9. IntelliJ IDEA 常用设置讲解2

    IntelliJ IDEA 有很多人性化的设置我们必须单独拿出来讲解,也因为这些人性化的设置让我们这些 IntelliJ IDEA 死忠粉更加死心塌地使用它和分享它. 常用设置 如上图 Gif 所示, ...

  10. 30个最常用css选择器解析(zz)

    你也许已经掌握了id.class.后台选择器这些基本的css选择器.但这远远不是css的全部.下面向大家系统的解析css中30个最常用的选择器,包括我们最头痛的浏览器兼容性问题.掌握了它们,才能真正领 ...