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

Android进阶之路系列:http://blog.csdn.net/column/details/16488.html

我们知道可以通过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进阶之路系列:http://blog.csdn.net/column/details/16488.html

你可能感兴趣的:(Android中Intent/Bundle的通信原理及大小限制(Parcelable原理及与Serializable的区别))