为什么Android为序列化对象提供了两个接口?序列化对象与Android Binder和AIDL文件interopt ?
当前回答
If you want to be a good citizen, take the extra time to implement Parcelable since it will perform 10 times faster and use less resources. However, in most cases, the slowness of Serializable won’t be noticeable. Feel free to use it but remember that serialization is an expensive operation so keep it to a minimum. If you are trying to pass a list with thousands of serialized objects, it is possible that the whole process will take more than a second. It can make transitions or rotation from portrait to lanscape feel very sluggish.
来源:http://www.developerphil.com/parcelable-vs-serializable/
其他回答
我的回答有点晚了,但我希望它能帮助到其他人。
在速度方面,可打包>序列化。但是,自定义Serializable是个例外。它几乎在Parcelable的范围内,甚至更快。
参考资料:https://www.geeksforgeeks.org/customized-serialization-and-deserialization-in-java/
例子:
要序列化的自定义类
class MySerialized implements Serializable {
String deviceAddress = "MyAndroid-04";
transient String token = "AABCDS"; // sensitive information which I do not want to serialize
private void writeObject(ObjectOutputStream oos) throws Exception {
oos.defaultWriteObject();
oos.writeObject("111111" + token); // Encrypted token to be serialized
}
private void readObject(ObjectInputStream ois) throws Exception {
ois.defaultReadObject();
token = ((String) ois.readObject()).subString(6); // Decrypting token
}
}
你可以在intent中使用serializable对象,但是在使serialize成为Parcelable对象的时候,它会给出一个严重的异常,比如NotSerializableException。不建议使用serializable和Parcelable。所以最好是扩展Parcelable与你想要使用的对象捆绑和意图。由于这个Parcelable是android特定的,所以它没有任何副作用。 :)
打包比使用Binder序列化快得多,因为序列化使用反射并导致许多GC。Parcelable是为了优化传递对象而设计的。
这是参考链接。 http://www.developerphil.com/parcelable-vs-serializable/
在编组和解组方面存在一些性能问题。Parcelable比Serializable快两倍。
请浏览以下连结:
http://www.3pillarglobal.com/insights/parcelable-vs-java-serialization-in-android-app-development
Serializable是一个标准的Java接口。您只需通过实现接口将类标记为Serializable, Java将在某些情况下自动序列化它。
Parcelable是一个Android特定的接口,你可以自己实现序列化。创建它的目的是要比Serializable高效得多,并解决默认Java序列化方案的一些问题。
我相信Binder和AIDL可以用于可打包对象。
但是,你可以在intent中使用Serializable对象。
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。