omg 今天遇見(jiàn)了一個(gè) 非常神奇的bug
解決辦法直接拉到最下面
一個(gè)行很常見(jiàn)的 獲取已經(jīng)安裝的apk信息
PackageManager pms = context.getPackageManager();
List<PackageInfo> packinfos = pms
.getInstalledPackages(PackageManager.GET_PERMISSIONS);
報(bào)出了
如下
Android develo 對(duì)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事務(wù)失敗忙上,因?yàn)樗罅恕?/p>
在遠(yuǎn)程過(guò)程調(diào)用期間赦拘,調(diào)用的參數(shù)和返回值將作為[Parcel](https://developer.android.com/reference/android/os/Parcel.html)
存儲(chǔ)在Binder事務(wù)緩沖區(qū)中的對(duì)象進(jìn)行傳輸燃异。如果參數(shù)或返回值太大而不適合事務(wù)緩沖區(qū)杨名,則調(diào)用將失敗[TransactionTooLargeException](https://developer.android.com/reference/android/os/TransactionTooLargeException.html)
并將被拋出。
Binder事務(wù)緩沖區(qū)具有有限的固定大小士骤,當(dāng)前為1Mb瓦胎,由進(jìn)程正在進(jìn)行的所有事務(wù)共享。因此衬鱼,即使大多數(shù)單個(gè)事務(wù)的大小適中,當(dāng)有許多事務(wù)正在進(jìn)行時(shí)憔杨,也會(huì)拋出此異常鸟赫。
遠(yuǎn)程過(guò)程調(diào)用拋出時(shí)有兩種可能的結(jié)果 [TransactionTooLargeException](https://developer.android.com/reference/android/os/TransactionTooLargeException.html)
∠穑客戶(hù)端無(wú)法將其請(qǐng)求發(fā)送到服務(wù)(很可能抛蚤,如果參數(shù)太大而無(wú)法放入事務(wù)緩沖區(qū)中),或者服務(wù)無(wú)法將其響應(yīng)發(fā)送回客戶(hù)端(最有可能的話(huà)寻狂,如果返回值為太大而不適合事務(wù)緩沖區(qū))岁经。無(wú)法確定實(shí)際發(fā)生了哪些結(jié)果∩呷客戶(hù)應(yīng)該假設(shè)發(fā)生了部分故障缀壤。
避免的關(guān)鍵[TransactionTooLargeException](https://developer.android.com/reference/android/os/TransactionTooLargeException.html)
是保持所有交易相對(duì)較小。嘗試最小[Parcel](https://developer.android.com/reference/android/os/Parcel.html)
化為參數(shù)創(chuàng)建所需的內(nèi)存量以及遠(yuǎn)程過(guò)程調(diào)用的返回值纠亚。避免傳輸大量字符串或大位圖塘慕。如果可能的話(huà),嘗試將大量請(qǐng)求分解成更小的部分蒂胞。
如果要實(shí)現(xiàn)服務(wù)图呢,則可能有助于對(duì)客戶(hù)端可以執(zhí)行的查詢(xún)施加大小或復(fù)雜性約束。例如啤誊,如果結(jié)果集可能變大岳瞭,則不允許客戶(hù)端一次請(qǐng)求多個(gè)記錄∥们拢或者瞳筏,不是一次性返回所有可用數(shù)據(jù),而是首先返回基本信息牡昆,然后根據(jù)需要讓客戶(hù)端請(qǐng)求其他信息姚炕。
很不幸的告訴你 我也沒(méi)有好的辦法
但是會(huì)有一點(diǎn)建議
參考
同時(shí)我在github也找到一個(gè)方案
github建議方案
首先避免獲取全部的信息
如果要判斷某個(gè)app是否安裝 ,方式如下,,
private boolean packageExists(PackageManager packageManager, String packageName) {
try {
packageManager.getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException ignored) {
return false;
}
}
如果非要獲取全部的信息 在遍歷 祝你好運(yùn)了 ,