​我们知道可以通过Intent和bundle在activity或fragment间进行通信,那么这个通信是如何实现的。
通过intent的bundle的源码可以看到它们都是实现了Parcelable,其实就是通过序列化来实现通信的。

提到Parcelable就不得不提Serializable,这里引用一段网上的总结:

介绍Parcelable不得不先提一下Serializable接口,Serializable是Java为我们提供的一个标准化的序列化接口,那什么是序列化呢? —- 简单来说就是将对象转换为可以传输的二进制流(二进制序列)的过程,这样我们就可以通过序列化,转化为可以在网络传输或者保存到本地的流(序列),从而进行传输数据 ,那反序列化就是从二进制流(序列)转化为对象的过程.

Parcelable是Android为我们提供的序列化的接口,Parcelable相对于Serializable的使用相对复杂一些,但Parcelable的效率相对Serializable也高很多,这一直是Google工程师引以为傲的,有时间的可以看一下Parcelable和Serializable的效率对比 Parcelable vs Serializable 号称快10倍的效率

Parcelable的底层使用了Parcel机制。传递实际上是使用了binder机制,binder机制会将Parcel序列化的数据写入到一个共享内存中,读取时也是binder从共享内存中读出字节流,然后Parcel反序列化后使用。这就是Intent或Bundle能够在activity或者跨进程通信的原理。(参考“探索startActivity流程及在Activity间是如何传递Intent的”)

关于Parcelable和Serializable的区别,同样引用一段网上的总结:

Parcelable和Serializable的区别和比较
Parcelable和Serializable都是实现序列化并且都可以用于Intent间传递数据,Serializable是Java的实现方式,可能会频繁的IO操作,所以消耗比较大,但是实现方式简单 Parcelable是Android提供的方式,效率比较高,但是实现起来复杂一些 , 二者的选取规则是:内存序列化上选择Parcelable, 存储到设备或者网络传输上选择Serializable(当然Parcelable也可以但是稍显复杂)

这样我们了解了Intent/Bundle的通信原理,但是使用时我们经常会遇到数据过大的问题,一般我们都知道intent不能传输大数据,这个限制是1MB,那么是什么或在哪里限制了呢?
当我们用Intent传输过大数据时,一般logcat会报出TransactionTooLargeException错误,谷歌官方对这个错误的描述如下:

The Binder transaction failed because it was too large.
During a remote procedure call, the arguments and the return value of the call are transferred as
Parcel objects stored in the Binder transaction buffer. If the arguments or the return value are too
large to fit in the transaction buffer, then the call will fail and TransactionTooLargeException
will be thrown.

The Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all
transactions in progress for the process. Consequently this exception can be thrown when there
are many transactions in progress even when most of the individual transactions are of moderate size.

There are two possible outcomes when a remote procedure call throws TransactionTooLargeException.
Either the client was unable to send its request to the service (most likely if the arguments were
too large to fit in the transaction buffer), or the service was unable to send its response back to
the client (most likely if the return value was too large to fit in the transaction buffer). It is
not possible to tell which of these outcomes actually occurred. The client should assume that a
partial failure occurred.

The key to avoiding TransactionTooLargeException is to keep all transactions relatively small.
Try to minimize the amount of memory needed to create a Parcel for the arguments and the return
value of the remote procedure call. Avoid transferring huge arrays of strings or large bitmaps.
If possible, try to break up big requests into smaller pieces.

If you are implementing a service, it may help to impose size or complexity contraints on the
queries that clients can perform. For example, if the result set could become large, then don't
allow the client to request more than a few records at a time. Alternately, instead of returning
all of the available data all at once, return the essential information first and make the client
ask for additional information later as needed.

简单来说,我们上面提到了Parcel机制使用了一个共享内存,这个共享内存就叫Binder transaction buffer,这块内存有一个大小限制,目前是1MB,而且是共用的,当超过了这个大小就会报错。
也就是说不仅仅是一次性传递大数据会出问题,当同时传递很多数据,尽管每个都不超过1MB,但是总大小超过1MB也会出错。

Android中Intent/Bundle的通信原理及大小限制(Parcelable原理及与Serializable的区别)相关推荐

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

    这篇文章转自博客园 Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putPa ...

  2. android集合方法,android中intent传递list或者对象的方法

    本文实例讲述了android中intent传递list或者对象的方法.分享给大家供大家参考.具体实现方法如下: 方法一: 如果单纯的传递List 或者List的话 就可以直接使用 代码如下: inte ...

  3. android 服务端 servlet webservice,Tomcat 上如何部署Servlet及Android中如何与服务器通信(12页)-原创力文档...

    Tomcat 上如何部署Servlet及Android中如何与服务器通信 下载Tomcat并安装 Apache Tomcat powers numerous large-scale, mission- ...

  4. Android中Intent介绍

    Android中Intent主要分为显式Intent和隐式Intent,今天会主要讲隐式Intent 一.显式Intent 最常用的就是下面这种形式 Intent intent = new Inten ...

  5. 从Android中Activity之间的通信说开来

    引言 最近两个星期在研究android的应用开发,学习了android应用开发的基础知识,基本控件,基本布局,基本动画效果,数据存储,http访问internet等等基础知识. android中有一个 ...

  6. Android中Intent的显示和隐式使用

    Android应用程序中组件之间的通信都少不了Intent的使用,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件, ...

  7. Android中的跨进程通信方法实例及特点分析(二):ContentProvider

    1.ContentProvider简单介绍 在Android中有些数据(如通讯录.音频.视频文件等)是要供非常多应用程序使用的.为了更好地对外提供数据,Android系统给我们提供了Content P ...

  8. 【Android】Android中Intent的用法总结

    转载▼ 来源:http://blog.sina.com.cn/s/blog_5f1fe33f0100n5e1.html  Intent只在Android中特有,我把它比作一种运载工具,就像飞机一样,会 ...

  9. Android中Intent传递Object和ArrayListObject对象---笔记

    首先看一下Intent的官方的API. 传递一些基本类型数据的方法如下: putExtra(String name, int value) putExtra(String name, String v ...

最新文章

  1. mqtt session保持 订阅消息_如何使用 MQTT 报文实现发布订阅功能
  2. CSV XLS文件区别
  3. Nature:麻省理工人造「巨型原子」问世,量子处理和量子通信合二为一
  4. Java动态代理的应用
  5. 【进展】不只是感谢信,物联网平台哪家强?冰箱运行监测系统项目重启
  6. 白话Elasticsearch45-深入聚合数据分析之易并行聚合算法,三角选择原则,近似聚合算法
  7. 不停应用重启oracle数据库,此类情况下解决Oracle数据库重启的四种方案
  8. wpf template的code写法
  9. java多线程w3c_多线程
  10. 网络编程(32)—— linux中销毁僵尸进程的四种方法
  11. jar打包 剔除第三方依赖以及它的依赖_面试官:为什么Spring Boot的jar可以直接运行?...
  12. mongodb如何写入图片_CTO之瞳-数据库-MongoDB
  13. nodejs字符与字节之间的转换
  14. CPython 标准库源码分析 collections.Counter
  15. php脚本搭vps,#分享#基于宝塔面板的ZFaka(发卡程序)一键脚本
  16. 事务,动态代理,连接池
  17. 苹果xsmax怎么开机_苹果XS/xs max按钮浮标怎么设置?
  18. App Store上的健身应用
  19. Zynga公布2021年第一季度财务业绩
  20. 【独家】蒋步星:慎思笃行,数据创业者的真实一面

热门文章

  1. 计算机专业毕业了,还要不要参加培训班?——Leo网上答疑(8)
  2. 高创新出GoTVbox多路电视解调器
  3. Node.Js同步mongdb数据
  4. 使用ImitateLogin模拟登录百度
  5. 剑指offer试题(PHP篇一)
  6. 08. 函数和流程控制
  7. SpringMVC解决前台传入的数组或集合类型数据
  8. C#语言实现定时开启或禁用网卡小程序
  9. 切莫开一块地荒一块地
  10. 通过OleDB连接方式,访问Access,Excel数据库.