How to prevent TransactionTooLargeException, or re

2019-08-19 16:04发布

I am sending data via broadcasts like this:

Intent outIntent = new Intent(Const.ACTION_FEED);
outIntent.putExtra(Const.EXTRA_FEED, data);
sendBroadcast(outIntent);

The issue is that data can get quite large, resulting in a TransactionTooLargeException. The documentation says:

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.

Bottom line: it seems to be impossible to tell in advance what size is acceptable for data.

Furthermore:

The key to avoiding TransactionTooLargeException is to keep all transactions relatively small. [...] If possible, try to break up big requests into smaller pieces.

The nature of the data I am sending is such that I could easily break it down into smaller pieces and send them individually, once I have established that the whole thing is too big to send at once.

The logical step would be to wrap the whole code in a try/catch block, and upon receiving a TransactionTooLarge exception, chop up the data into smaller chunks and retry.

Alas, according to the logcat, the exception is not thrown at the caller’s end but in a system process. The system then goes on to crash the receiver of the broadcast, at which point any recovery is out of the sender’s control.

How can I tell how much data is OK to send as a broadcast extra, and prevent crashing the receiver of the data?

0条回答
登录 后发表回答