How to broadcast a parcelable object in an intent?

南笙酒味 提交于 2019-12-05 17:56:04

When you read a byte array, you need to have previously allocated a byte array of (at least) the correct size. Like this:

byte[] blah = new byte[100];
in.readByteArray(blah);

Also, you don't want to use writeValue() to write primitives to the Parcel. Use the appropriate methods. Example:

out.writeInt(id);    // int = 256
out.writeInt(len);   // int = 2, length of byte array
out.writeByteArray(data);  // byte array = { 17, 51 }

You must always pair the write() methods with the same type of read() methods on the parcel. For example, if you use writeValue() to write something into the Parcel, you must read it using readValue(). If you know that the variable is an int, then you should write it using writeInt() and read it using readInt(). If you write it using writeValue() then the "type" of the object is also written to the Parcel so that readValue() will know what type of object it is. This is why you are seeing extra data in the Parcel.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!