【发布时间】:2018-12-27 17:47:52
【问题描述】:
在通过单个 APK 运行的两个 Android 进程之间发送消息时,我收到了 TransactionTooLargeException。每条消息只包含少量数据,much smaller than the 1 mb total (as specified in the docs)。
我创建了一个测试应用程序(下面的代码)来解决这种现象,并注意到三件事:
如果每条消息超过 200 kb,我会收到
android.os.TransactionTooLargeException。如果每条消息小于 200kb,我会收到
android.os.DeadObjectException添加
Thread.sleep(1)似乎已经解决了这个问题。我无法使用Thread.sleep 获得任何异常
Looking through the Android C++ code,似乎transaction 因未知原因而失败并被解释为这些异常之一
问题
- 什么是“
transaction”? - 什么定义了事务中的内容?在给定时间内是否有一定数量的事件?或者只是事件的最大数量/规模?
- 有没有办法“刷新”交易或等待交易完成?
- 避免这些错误的正确方法是什么? (注意:将其分解成更小的部分只会引发不同的异常)
代码
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.boundservicestest"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".BoundService" android:process=":separate"/>
</application>
</manifest>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var sendDataButton: Button
private val myServiceConnection: MyServiceConnection = MyServiceConnection(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
myServiceConnection.bind()
sendDataButton = findViewById(R.id.sendDataButton)
val maxTransactionSize = 1_000_000 // i.e. 1 mb ish
// Number of messages
val n = 10
// Size of each message
val bundleSize = maxTransactionSize / n
sendDataButton.setOnClickListener {
(1..n).forEach { i ->
val bundle = Bundle().apply {
putByteArray("array", ByteArray(bundleSize))
}
myServiceConnection.sendMessage(i, bundle)
// uncommenting this line stops the exception from being thrown
// Thread.sleep(1)
}
}
}
}
MyServiceConnection.kt
class MyServiceConnection(private val context: Context) : ServiceConnection {
private var service: Messenger? = null
fun bind() {
val intent = Intent(context, BoundService::class.java)
context.bindService(intent, this, Context.BIND_AUTO_CREATE)
}
override fun onServiceConnected(name: ComponentName, service: IBinder) {
val newService = Messenger(service)
this.service = newService
}
override fun onServiceDisconnected(name: ComponentName?) {
service = null
}
fun sendMessage(what: Int, extras: Bundle? = null) {
val message = Message.obtain(null, what)
message.data = extras
service?.send(message)
}
}
BoundService.kt
internal class BoundService : Service() {
private val serviceMessenger = Messenger(object : Handler() {
override fun handleMessage(message: Message) {
Log.i("BoundService", "New Message: ${message.what}")
}
})
override fun onBind(intent: Intent?): IBinder {
Log.i("BoundService", "On Bind")
return serviceMessenger.binder
}
}
build.gradle*
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.boundservicestest"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.1'
}
堆栈跟踪
07-19 09:57:43.919 11492-11492/com.example.boundservicestest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.boundservicestest, PID: 11492
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:448)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:764)
at android.os.IMessenger$Stub$Proxy.send(IMessenger.java:89)
at android.os.Messenger.send(Messenger.java:57)
at com.example.boundservicestest.MyServiceConnection.sendMessage(MyServiceConnection.kt:32)
at com.example.boundservicestest.MainActivity$onCreate$1.onClick(MainActivity.kt:30)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
【问题讨论】:
-
我可以在 Google Pixel 模拟器 - 具有 64 位架构的 Android 8.1 中运行您的代码(没有任何错误),无需 Thread.sleep(1)。跨度>
-
@PravinDivraniya,您是否包括了
android:process=":separate"?只有在单独的进程中运行服务时才会失败。你也有真正的手机可以测试吗? -
是的,我在不同的进程上运行我的服务。
<service android:name=".BoundService" android:process=":myprocess"/> -
我还在真实设备 moto G5 plus 上进行了测试 - Android 7.0 及其工作。
-
@PravinDivraniya 你的 sdk、目标 sdk 和 minsdk 是什么?我刚刚添加了我的
build.gradle。我每次在运行 Android P 的 Google Pixel XL Android 8.1 和 Google Pixel 2 XL 上都会崩溃
标签: android transactions android-service ipc android-binder