Android Intent傳遞數(shù)據(jù)大小限制詳解
前言
在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í)價值,謝謝大家對腳本之家的支持。
- Android顯式Intent與隱式Intent的使用詳解
- Android Intent傳遞大量數(shù)據(jù)出現(xiàn)問題解決
- Android開發(fā)Intent跳轉(zhuǎn)傳遞list集合實(shí)現(xiàn)示例
- Android13?加強(qiáng)Intent?filters?的安全性
- android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計算
- Android使用Intent的Action和Data屬性實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面
- Android開發(fā)中Intent.Action各種常見的作用匯總
- Android使用Intent隱式實(shí)現(xiàn)頁面跳轉(zhuǎn)
- Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的兩種方法
- Android Intent基礎(chǔ)用法及作用詳解
相關(guān)文章
Flutter版本的自定義短信驗(yàn)證碼實(shí)現(xiàn)示例解析
這篇文章主要介紹了Flutter版本的自定義短信驗(yàn)證碼實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08android listview優(yōu)化幾種寫法詳細(xì)介紹
這篇文章只是總結(jié)下getView里面優(yōu)化視圖的幾種寫法,需要的朋友可以參考下2012-11-11android表格效果之ListView隔行變色實(shí)現(xiàn)代碼
首先繼承SimpleAdapter再使用重載的Adapter來達(dá)到效果,其實(shí)主要是需要重載SimpleAdapter,感興趣的朋友可以研究下,希望本文可以幫助到你2013-02-02Android圖片選擇器ImageEditContainer
這篇文章主要為大家詳細(xì)介紹了Android圖片選擇器ImageEditContainer的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Android中實(shí)現(xiàn)ProgressBar菊花旋轉(zhuǎn)進(jìn)度條的動畫效果
大家在一些頁面經(jīng)常會遇到加載中需要顯示一個加載動畫,像旋轉(zhuǎn)的菊花旋轉(zhuǎn)的圈圈動畫效果,本文通過實(shí)例代碼給大家講解下,需要的朋友參考下吧2021-09-09