为什么Android为序列化对象提供了两个接口?序列化对象与Android Binder和AIDL文件interopt ?
当前回答
Parcelable将对象转换为字节流,以便在Android的进程之间传递对象。
序列化将POJO转换为字符串(JSON字符串),并用于跨平台传输对象信息。
其他回答
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是更好的选择,因为谷歌推荐它,你可以从这个帖子中看到,它更受欢迎。
1. 可序列化的
接口是一个标记(没有抽象方法的接口),不需要重新定义任何东西。
2. Parcelable
具有抽象方法的接口。在实现它的时候,你需要重新定义所有的抽象方法,指定哪些字段以及你需要写/读的顺序(通常工作室自己可以生成它们)。
实际上没有人用Kotlin编写。对此有一个特殊的注释,由于它,这个接口的实现将自动生成。要使用它,你需要添加一个特殊的插件。
在构建。Gradle插件部分,添加另一个插件:id 'kotlin-parcelize'
同步项目
您不必担心实现方法,您所需要的只是实现Parcelable接口并添加@Parcelize注释。
一切都会好的,工作很快!
结果
如果实现Parcelable接口而不是Serializable接口,实现过程会更快。
在Android中,我们不能只是将对象传递给活动。要做到这一点,对象必须实现Serializable或Parcelable接口。
可序列化的
Serializable是一个标准的Java接口。你可以实现Serializable接口并添加重写方法。这种方法的问题是使用了反射,而且它是一个缓慢的过程。此方法创建了大量临时对象,并导致大量垃圾收集。然而,Serializable接口更容易实现。
请看下面的例子(Serializable):
// MyObjects Serializable class
import java.io.Serializable;
import java.util.ArrayList;
import java.util.TreeMap;
import android.os.Parcel;
import android.os.Parcelable;
public class MyObjects implements Serializable {
private String name;
private int age;
public ArrayList<String> address;
public MyObjects(String name, int age, ArrayList<String> address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public ArrayList<String> getAddress() {
if (!(address == null))
return address;
else
return new ArrayList<String>();
}
public String getName() {
return name;
}
// return age
public int getAge() {
return age;
}
}
// MyObjects instance
MyObjects mObjects = new MyObjects("name", "age", "Address array here");
// Passing MyObjects instance via intent
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);
// Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getSerializableExtra("UniqueKey");
Parcelable
Parcelable进程比Serializable快得多。原因之一是我们明确了序列化过程,而不是使用反射来推断它。出于这个目的,代码进行了大量优化,这也是合情合理的。
看看下面的例子(Parcelable):
// MyObjects Parcelable class
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class MyObjects implements Parcelable {
private int age;
private String name;
private ArrayList<String> address;
public MyObjects(String name, int age, ArrayList<String> address) {
this.name = name;
this.age = age;
this.address = address;
}
public MyObjects(Parcel source) {
age = source.readInt();
name = source.readString();
address = source.createStringArrayList();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(age);
dest.writeString(name);
dest.writeStringList(address);
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public ArrayList<String> getAddress() {
if (!(address == null))
return address;
else
return new ArrayList<String>();
}
public static final Creator<MyObjects> CREATOR = new Creator<MyObjects>() {
@Override
public MyObjects[] newArray(int size) {
return new MyObjects[size];
}
@Override
public MyObjects createFromParcel(Parcel source) {
return new MyObjects(source);
}
};
}
// MyObjects instance
MyObjects mObjects = new MyObjects("name", "age", "Address array here");
// Passing MyOjects instance
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);
// Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getParcelableExtra("UniqueKey");
你可以传递Parcelable对象的ArrayList,如下所示:
// Array of MyObjects
ArrayList<MyObjects> mUsers;
// Passing MyOjects instance
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putParcelableArrayListExtra("UniqueKey", mUsers);
startActivity(mIntent);
// Getting MyObjects instance
Intent mIntent = getIntent();
ArrayList<MyObjects> mUsers = mIntent.getParcelableArrayList("UniqueKey");
结论
Parcelable接口比Serializable接口快 与可串行化接口相比,可打包接口需要更多的时间来实现 可序列化的接口更容易实现 可序列化接口创建了大量临时对象,并导致大量垃圾收集 Parcelable数组可以通过Intent在android中传递
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/
推荐文章
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?