欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Intent傳遞數(shù)據(jù)大小限制詳解

 更新時間:2019年04月11日 08:31:19   作者:rf_dev  
這篇文章主要給大家介紹了關(guān)于Android Intent傳遞數(shù)據(jù)大小限制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

在sendBroadcast,startActivity時,我們會用到Intent。

Intent可以攜帶一些數(shù)據(jù),比如基本類型數(shù)據(jù)int、Boolean,或是String,或是序列化對象,Parcelable與Serializable。
Intent傳遞數(shù)據(jù)時,如果數(shù)據(jù)太大,可能會出現(xiàn)異常。比如App閃退,或是Intent發(fā)送不成功,logcat報錯等等。

這就牽涉到一個問題:Intent 傳遞數(shù)據(jù)大小限制。

Intent到底能夠攜帶多少數(shù)據(jù)呢?

使用Intent傳送數(shù)據(jù)時,可能會出現(xiàn)異常

在Intent中傳入一個Parcelable對象;例如傳入一個bitmap對象。

代碼參考: github.com/RustFisher/

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

選擇bitmap的原因是,Bitmap實(shí)現(xiàn)了Parcelable接口,并且可以通過getByteCount()得知所占內(nèi)存大小。

sendBroadcast時,報出如下信息

 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)

查看異常類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有關(guān)。

Intent攜帶信息的大小受Binder限制

Intent攜帶信息的大小其實(shí)是受Binder限制。本文標(biāo)題也可以改為“Binder傳遞數(shù)據(jù)大小限制”。

數(shù)據(jù)以Parcel對象的形式存放在Binder傳遞緩存中。

如果數(shù)據(jù)或返回值比傳遞buffer大,則此次傳遞調(diào)用失敗并拋出TransactionTooLargeException異常。

Binder傳遞緩存有一個限定大小,通常是1Mb。但同一個進(jìn)程中所有的傳輸共享緩存空間。

多個地方在進(jìn)行傳輸時,即時它們各自傳輸?shù)臄?shù)據(jù)不超出大小限制,TransactionTooLargeException異常也可能會被拋出。

在使用Intent傳遞數(shù)據(jù)時,1Mb并不是安全上限。因?yàn)锽inder中可能正在處理其它的傳輸工作。

不同的機(jī)型和系統(tǒng)版本,這個上限值也可能會不同。

在其它地方,例如onSaveInstanceState(@NonNull Bundle outState),也可能會遇到與Binder有關(guān)的類似問題。

為什么Binder要限制傳輸數(shù)據(jù)的大小

個人推測,作為一種IPC的方式,Binder并不是為傳輸大量數(shù)據(jù)而設(shè)計。

傳輸大量數(shù)據(jù),可以考慮URL之類的方法。

參考

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論