Android Intent 传递大量数据问题

惯例先贴出官方文档
developer.android.google.cn/guide/compo…

sendBroadcaststartActivity时,我们会用到Intent。 Intent可以携带一些数据,比如基本类型数据int、Boolean,或是String,或是序列化对象,Parcelable与Serializable。

Intent传递数据时,如果数据太大,可能会出现异常TransactionTooLargeException

注意:在 Android 7.0(API 级别 24)或更高版本中,系统会在运行时抛出 TransactionTooLargeException 异常。在较低版本的 Android 中,系统仅在 logcat 中显示警告。

TransactionTooLargeException继承了RemoteException

package android.os;
public class TransactionTooLargeException extends RemoteException {
    public TransactionTooLargeException() {
        super();
    }

    public TransactionTooLargeException(String msg) {
        super(msg);
    }
}
复制代码

追踪到Binder,它的transactNative方法会报出RemoteException

public native boolean transactNative(int code, Parcel data, Parcel reply,
            int flags) throws RemoteException;
复制代码

抛出异常与Binder有关。

通过 intent 发送数据时,应小心地将数据大小限制为几 KB。发送过多数据会导致系统抛出 TransactionTooLargeException 异常。

Intent携带信息的大小受Binder限制

Intent携带信息的大小其实是受Binder限制。本文标题也可以改为“Binder传递数据大小限制”。

数据以Parcel对象的形式存放在Binder传递缓存中。 如果数据或返回值比传递buffer大,则此次传递调用失败并抛出TransactionTooLargeException异常。

Binder事务缓冲区有一个限定大小,通常是1Mb。由进程中正在处理的所有事务共享缓存空间。

由于此限制是进程级别而不是 Activity 级别的限制,因此这些事务包括应用中的所有 binder 事务,例如 onSaveInstanceStatestartActivity 以及与系统的任何互动。超过大小限制时,将引发 TransactionTooLargeException

对于 savedInstanceState 的具体情况,应将数据量保持在较小的规模,因为只要用户可以返回到该 Activity,系统进程就需要保留所提供的数据(即使 Activity 的进程已终止)。我们建议您将保存的状态保持在 50k 数据以下。

为什么Binder要限制传输数据的大小

个人推测,作为一种IPC的方式,Binder并不是为传输大量数据而设计。

替代方案

当需要传递长字符串、Bitmap等时,不要考虑使用Intent传递数据的方案

1、单例

2、EventBus

3、Application

4、持久化数据


作者:AnRFDev
链接:juejin.cn/post/684490… 来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自juejin.im/post/7067846053042585637