为什么Android为序列化对象提供了两个接口?序列化对象与Android Binder和AIDL文件interopt ?


当前回答

Parcelable是Android开发中的一种标准。但不是因为速度

Parcelable是数据传输的推荐方法。但是如果你正确地使用serializable,你会发现serializable有时甚至比parcelable还要快。或者至少时间是可以比较的。

Parcelable比Serializable快吗?

不,如果序列化操作正确的话。

Usual Java serialization on an average Android device (if done right *) is about 3.6 times faster than Parcelable for writes and about 1.6 times faster for reads. Also it proves that Java Serialization (if done right) is fast storage mechanism that gives acceptable results even with relatively large object graphs of 11000 objects with 10 fields each. * The sidenote is that usually everybody who blindly states that "Parcelable is mush faster" compares it to default automatic serialization, which uses much reflection inside. This is unfair comparison, because Parcelable uses manual (and very complicated) procedure of writing data to the stream. What is usually not mentioned is that standard Java Serializable according to the docs can also be done in a manual way, using writeObject() and readObject() methods. For more info see JavaDocs. This is how it should be done for the best performance.

所以,如果serializable更快更容易实现,为什么android有parcelable呢?

原因在于本地代码。创建Parcelable不仅仅是为了进程间通信。它也可以用于代码间通信。你可以从c++原生层发送和接收对象。就是这样。

你应该选择什么?两者都很有效。但我认为Parcelable是更好的选择,因为谷歌推荐它,你可以从这个帖子中看到,它更受欢迎。

其他回答

Serializable是一个标准的Java接口。您只需通过实现接口将类标记为Serializable, Java将在某些情况下自动序列化它。

Parcelable是一个Android特定的接口,你可以自己实现序列化。创建它的目的是要比Serializable高效得多,并解决默认Java序列化方案的一些问题。

我相信Binder和AIDL可以用于可打包对象。

但是,你可以在intent中使用Serializable对象。

Serializable接口可以像Parcelable接口一样使用,从而获得(不是很多)更好的性能。 只需覆盖这两个方法来处理手动编组和解组过程:

private void writeObject(java.io.ObjectOutputStream out)
    throws IOException
private void readObject(java.io.ObjectInputStream in)
    throws IOException, ClassNotFoundException

不过,在我看来,在开发原生Android时,使用Android api是正确的选择。

看到的:

https://bitbucket.org/afrishman/androidserializationtest/ https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

Parcelable是Android开发中的一种标准。但不是因为速度

Parcelable是数据传输的推荐方法。但是如果你正确地使用serializable,你会发现serializable有时甚至比parcelable还要快。或者至少时间是可以比较的。

Parcelable比Serializable快吗?

不,如果序列化操作正确的话。

Usual Java serialization on an average Android device (if done right *) is about 3.6 times faster than Parcelable for writes and about 1.6 times faster for reads. Also it proves that Java Serialization (if done right) is fast storage mechanism that gives acceptable results even with relatively large object graphs of 11000 objects with 10 fields each. * The sidenote is that usually everybody who blindly states that "Parcelable is mush faster" compares it to default automatic serialization, which uses much reflection inside. This is unfair comparison, because Parcelable uses manual (and very complicated) procedure of writing data to the stream. What is usually not mentioned is that standard Java Serializable according to the docs can also be done in a manual way, using writeObject() and readObject() methods. For more info see JavaDocs. This is how it should be done for the best performance.

所以,如果serializable更快更容易实现,为什么android有parcelable呢?

原因在于本地代码。创建Parcelable不仅仅是为了进程间通信。它也可以用于代码间通信。你可以从c++原生层发送和接收对象。就是这样。

你应该选择什么?两者都很有效。但我认为Parcelable是更好的选择,因为谷歌推荐它,你可以从这个帖子中看到,它更受欢迎。

我的回答有点晚了,但我希望它能帮助到其他人。

在速度方面,可打包>序列化。但是,自定义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
    }

}

1. 可序列化的

接口是一个标记(没有抽象方法的接口),不需要重新定义任何东西。

2. Parcelable

具有抽象方法的接口。在实现它的时候,你需要重新定义所有的抽象方法,指定哪些字段以及你需要写/读的顺序(通常工作室自己可以生成它们)。

实际上没有人用Kotlin编写。对此有一个特殊的注释,由于它,这个接口的实现将自动生成。要使用它,你需要添加一个特殊的插件。

在构建。Gradle插件部分,添加另一个插件:id 'kotlin-parcelize'

同步项目

您不必担心实现方法,您所需要的只是实现Parcelable接口并添加@Parcelize注释。

一切都会好的,工作很快!

结果

如果实现Parcelable接口而不是Serializable接口,实现过程会更快。