android_os_TransactionTooLargeException

问题分析

Binder传输的数据存在Parcel类中,如果Binder的参数或返回值太大(超过1MB),不适合的事务缓冲区,调用会失败,并抛出TransactionTooLargeException。

解决方案

Binder的参数或返回值太大导致调用失败,并抛出TransactionTooLargeException,建议不要将大量数据传入Binder,避免传入大数组、大量字符串或位图。

代码示例

android.os.TransactionTooLargeException
    at android.os.BinderProxy.transact(Native Method)
    at com.android.internal.view.IInputMethodManager$Stub$Proxy.startInput(IInputMethodManager.java:604)
    at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1173)
    at android.view.inputmethod.InputMethodManager.checkFocus(InputMethodManager.java:1282)
    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3201)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
public class MainActivity extends Activity {
    private IDictionaryManager mDictionaryManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent();
        intent.setAction("android.intent.action.DictionaryManagerService");
        intent.setPackage("com.wanginbeijing.dictionaryserver");
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        //添加一个新单词
        findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    mDictionaryManager.add("你好", "Hello");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        //查询单词
        findViewById(R.id.btn_query).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String english = mDictionaryManager.query("你好");
                    Toast.makeText(MainActivity.this,english, Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            IDictionaryManager dictionaryManager = IDictionaryManager.Stub.asInterface(iBinder);
            try {
                mDictionaryManager = dictionaryManager;
                Toast.makeText(MainActivity.this, "connect success", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(MainActivity.this, "connect failed", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
}

参考文献