软件编程
位置:首页>> 软件编程>> Android编程>> Android Intent传递大量数据出现问题解决

Android Intent传递大量数据出现问题解决

作者:AnRFDev  发布时间:2021-11-07 10:16:13 

标签:Android,Intent,传递,大量数据

官方文档 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 事务,例如 onSaveInstanceStatestartActivity 以及与系统的任何互动。超过大小限制时,将引发 TransactionTooLargeException。

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

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

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

替代方案

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

1、单例

2、EventBus

3、Application

4、持久化数据

来源:https://juejin.cn/post/7067846053042585637

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com