In sendBroadcast, startActivity, we use Intent. Intents can carry data such as basic types int, Boolean, String, or serialized objects, Parcelable, and Serializable.

When an Intent passes data, an exception may occur if the data is too large. For example, App flashes back, or the Intent fails to be sent, logcat reports an error, and so on.

This brings up a problem: data size limits delivered by intEnts.

How much data can intEnts carry?

An exception may occur when data is transmitted using intEnts

Passing a Parcelable object in the Intent; For example, passing in a Bitmap object.

Code reference: github.com/RustFisher/…

    Bitmap b1 = Bitmap.createScaledBitmap(srcBmp, dstWid, dstHeight, false);
    Intent intent = new Intent(MSG_INTENT);
    intent.putExtra(K_PIC, b1);
Copy the code

Bitmaps were chosen because they implement the Parcelable interface and can be used to determine the amount of memory used by getByteCount().

When sending sendBroadcast, the following information is displayed

 V/ActivityManager: Broadcast: Intent { act=intent_bi flg=0x10 (has extras) } ordered=false userid=0 callerApp=ProcessRecord{27aeaaf5 31217:com.rustfisher.basic4/u0a113}
 E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
 W/BroadcastQueue: Failure sending broadcast Intent { act=intent_bi flg=0x10 (has extras) }
        android.os.TransactionTooLargeException
            at android.os.BinderProxy.transactNative(Native Method)
            at android.os.BinderProxy.transact(Binder.java:504)
            at android.app.ApplicationThreadProxy.scheduleRegisteredReceiver(ApplicationThreadNative.java:1170)
            at com.android.server.am.BroadcastQueue.performReceiveLocked(BroadcastQueue.java:576)
            at com.android.server.am.BroadcastQueue.deliverToRegisteredReceiverLocked(BroadcastQueue.java:848)
            at com.android.server.am.BroadcastQueue.processNextBroadcast(BroadcastQueue.java:917)
            at com.android.server.am.BroadcastQueue$BroadcastHandler.handleMessage(BroadcastQueue.java:254)
            at android.os.Handler.dispatchMessage(Handler.java:111)
            at android.os.Looper.loop(Looper.java:194)
            at android.os.HandlerThread.run(HandlerThread.java:61)
            at com.android.server.ServiceThread.run(ServiceThread.java:46)
Copy the code

Check the exception class TransactionTooLargeException, it inherits the remoteexceptions

package android.os;
public class TransactionTooLargeException extends RemoteException {
    public TransactionTooLargeException(a) {
        super(a); }public TransactionTooLargeException(String msg) {
        super(msg); }}Copy the code

Traced to Binder, whose transactNative method reports a RemoteException

public native boolean transactNative(int code, Parcel data, Parcel reply,
            int flags) throws RemoteException;
Copy the code

Throwing exceptions is related to Binder.

The size of information carried by intEnts is limited by binders

The size of information that intEnts can carry is limited by binders. The title of this article could have been “Binder Transfer Data Size Limits.”

The data is stored in a Binder delivery cache as Parcel objects. If the data or the return value is greater than transmit buffer, then pass the call fails and throw TransactionTooLargeException anomalies.

Binder delivery caches have a limited size, usually 1Mb. But all transports in the same process share cache space.

Multiple places in the transmission, real-time their transmission data does not exceed the size limit, TransactionTooLargeException exception may be thrown.

When passing data using intEnts, 1Mb is not the safe upper limit. The Binder may be handling other transmissions. The upper limit may be different for different models and system versions.

Similar problems with binders can be encountered elsewhere, such as onSaveInstanceState(@nonNULL Bundle outState).

Why do binders limit the size of data transferred

As a means of IPC, Binder was not designed to transmit large amounts of data.

To transfer large amounts of data, consider methods like urls.

reference

  • Stackoverflow.com/questions/8…
  • Developer.android.com/reference/a…