通过 Android Intent 传递协议缓冲区对象
任何可序列化或可解析对象都可以通过Intent进行交换。 但 protobuf 对象不实现 Serialized 或 Parceable。 由于 protobuf 对象的实现将来可能会发生变化,因此不建议修改/扩展它们。
如何通过Android Intent从协议缓冲区消息(生成的类)交换数据?
编辑:我使用的是protobuf-2.3.0,其中GenerateMessage 和 GeneeratedMessageLite 不实现 Serialized。生成的消息于 2010 年 11 月 2 日开始实现序列化。Protobuf-2.4.1于 2011 年 4 月发布。
Any Serializable or Parceable objects can be exchanged by Intent.
But protobuf objects do not implement Serializable or Parceable.
Since protobuf objects implementation may change in the future, it's not advised to modify/extend them.
How to exchange by Android Intent data from protocol buffer message (generated class) ?
EDIT : I was using protobuf-2.3.0 where GeneratedMessage and GeneratedMessageLite don't implement Serializable. Generated messages started to implement Serializable on 2 November 2010. Protobuf-2.4.1 was released on April 2011.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下答案在 2011 年是正确的,但
GeneeratedMessageLite
不再实现Serialized
。GenerateMessage
仍然如此。GenerateMessage
和GenerateMessageLite
都实现了Serializable
,因此据我所知,您应该能够序列化任何特定的生成消息类 意识到的。您是否尝试过仅序列化Message
?The following answer was true in 2011, but
GeneratedMessageLite
no longer implementsSerializable
.GeneratedMessage
still does.GeneratedMessage
andGeneratedMessageLite
both implementSerializable
, so you should be able to just serialize any specific generated message class as far as I'm aware. Were you trying to serialize justMessage
by any chance?目前,在 Java-lite 中(目前推荐用于 Android)执行此操作的最佳方法是将对象转换为 ByteArray 然后将其发送到 Activity 并将字节数组转换回目标 Activity 中的对象。
Java-nano 用于实现
Parcelable
,它可以与Android Parcelable
一起使用在 Activity 之间发送对象。然而,Java-nano 已不再存在,并且GeneeratedMessageLite
不实现Serializable
或Parcelable
。For now probably the best way to do this in Java-lite (currently recommended for Android) is convert the object to ByteArray and then send it to the Activity and convert the byte Array back to the object in the target activity.
Java-nano used to implement
Parcelable
which could be used withAndroid Parcelable
to send objects between Activities. However Java-nano is no longer, andGeneratedMessageLite
doesn't implement eitherSerializable
orParcelable
.