TransactionTooLargeException 解决办法

omg 今天遇见了一个 非常神奇的bug

解决办法直接拉到最下面

一个行很常见的 获取已经安装的apk信息

PackageManager pms = context.getPackageManager();
        List packinfos = pms
                .getInstalledPackages(PackageManager.GET_PERMISSIONS);

报出了
如下


TransactionTooLargeException 解决办法_第1张图片
4e11c84f9a3ddaed789b3a2d9b92d2a.png

Android develo 对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](https://developer.android.com/reference/android/os/Parcel.html)` 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](https://developer.android.com/reference/android/os/TransactionTooLargeException.html)` 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](https://developer.android.com/reference/android/os/TransactionTooLargeException.html)`. 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](https://developer.android.com/reference/android/os/TransactionTooLargeException.html)` is to keep all transactions relatively small. Try to minimize the amount of memory needed to create a `[Parcel](https://developer.android.com/reference/android/os/Parcel.html)` 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.

翻译
Binder事务失败,因为它太大了。

在远程过程调用期间,调用的参数和返回值将作为[Parcel](https://developer.android.com/reference/android/os/Parcel.html)存储在Binder事务缓冲区中的对象进行传输。如果参数或返回值太大而不适合事务缓冲区,则调用将失败[TransactionTooLargeException](https://developer.android.com/reference/android/os/TransactionTooLargeException.html)并将被抛出。

Binder事务缓冲区具有有限的固定大小,当前为1Mb,由进程正在进行的所有事务共享。因此,即使大多数单个事务的大小适中,当有许多事务正在进行时,也会抛出此异常。

远程过程调用抛出时有两种可能的结果 [TransactionTooLargeException](https://developer.android.com/reference/android/os/TransactionTooLargeException.html)。客户端无法将其请求发送到服务(很可能,如果参数太大而无法放入事务缓冲区中),或者服务无法将其响应发送回客户端(最有可能的话,如果返回值为太大而不适合事务缓冲区)。无法确定实际发生了哪些结果。客户应该假设发生了部分故障。

避免的关键[TransactionTooLargeException](https://developer.android.com/reference/android/os/TransactionTooLargeException.html)是保持所有交易相对较小。尝试最小[Parcel](https://developer.android.com/reference/android/os/Parcel.html)化为参数创建所需的内存量以及远程过程调用的返回值。避免传输大量字符串或大位图。如果可能的话,尝试将大量请求分解成更小的部分。

如果要实现服务,则可能有助于对客户端可以执行的查询施加大小或复杂性约束。例如,如果结果集可能变大,则不允许客户端一次请求多个记录。或者,不是一次性返回所有可用数据,而是首先返回基本信息,然后根据需要让客户端请求其他信息。

很不幸的告诉你 我也没有好的办法

但是会有一点建议

参考

stackoverflow建议方案

同时我在github也找到一个方案
github建议方案

首先避免获取全部的信息
如果要判断某个app是否安装 ,方式如下,,

private boolean packageExists(PackageManager packageManager, String packageName) {
        try {
            packageManager.getPackageInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException ignored) {
            return false;
        }
    }

如果非要获取全部的信息 在遍历 祝你好运了 ,

你可能感兴趣的:(TransactionTooLargeException 解决办法)