我正在尝试使用 ObjectInputStreams
等通过网络将 Bundle
从一台设备发送到另一台设备。问题是 Bundle
是不可序列化的。有没有一种简单的方法可以实现这一点?我想使用 Bundle
因为我可以发送各种类型(String、int、parcelables、列表等)
最佳答案
您不应该序列化 Bundle,因为它不能保证版本兼容性。因此,如果您可以确保每个设备都使用完全相同的版本,那么应该没问题。但我真的不会推荐这个。每个 api 都声明不应该这样做!
Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.
https://developer.android.com/reference/android/os/Parcel.html
但是如果你真的想这样做,这里有一个实现它的方法:
Bundle in = ...;
OutputStream os = ...
Parcel p = Parcel.obtain(); //create empty parcel object
in.writeToParcel(p, 0); //saving bundle as parcel
os.write(p.marshall()); //write parcel to stream
关于android - 通过网络发送 bundle ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212628/