Android Intent传递大量数据出现问题解决
作者:AnRFDev 时间:2021-11-07 10:16:13
官方文档 https://developer.android.google.cn/guide/components/activities/parcelables-and-bundles
在sendBroadcast,startActivity时,我们会用到Intent。 Intent可以携带一些数据,比如基本类型数据int、Boolean,或是String,或是序列化对象,Parcelable与Serializable。
异常TransactionTooLargeException
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 事务,例如 onSaveInstanceState,startActivity 以及与系统的任何互动。超过大小限制时,将引发 TransactionTooLargeException。
对于 savedInstanceState 的具体情况,应将数据量保持在较小的规模,因为只要用户可以返回到该 Activity,系统进程就需要保留所提供的数据(即使 Activity 的进程已终止)。我们建议您将保存的状态保持在 50k 数据以下。
为什么Binder要限制传输数据的大小
个人推测,作为一种IPC的方式,Binder并不是为传输大量数据而设计。
替代方案
当需要传递长字符串、Bitmap等时,不要考虑使用Intent传递数据的方案
1、单例
2、EventBus
3、Application
4、持久化数据
来源:https://juejin.cn/post/7067846053042585637
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android去除AlertDialog的按钮栏的分隔线
![](https://img.aspxhome.com/file/2023/9/94289_0s.png)
详解C# 线程的挂起与唤醒
Java重写equals及hashcode方法流程解析
mvc开启gzip压缩示例分享
Java中关于MouseWheelListener的鼠标滚轮事件详解
springboot以FTP方式上传文件到远程服务器
WebService教程详解(一)
详解Springboot之整合JDBCTemplate配置多数据源
简单阐述一下Java集合的概要
![](https://img.aspxhome.com/file/2023/0/58300_0s.png)
SpringMVC之@requestBody的作用及说明
Android SharedPreferences实现记住密码和自动登录界面
![](https://img.aspxhome.com/file/2023/9/83529_0s.png)
利用C#实现获取当前设备硬件信息
C#事件(event)使用方法详解
java实现打砖块小游戏
Java Idea高效率配置技巧实例解析
![](https://img.aspxhome.com/file/2023/7/61407_0s.jpg)