🔥 什么是序列化
由于存在于記憶體中的物件都是暫時的,無法長期駐存,為了把物件的狀態保持下來,這時需要把物件寫入到磁盤或者其他介質中,這個程序就叫做序列化,
🔥 為什么序列化
-
永久的保存物件資料(將物件資料保存在檔案當中,或者是磁盤中),
-
物件在網路中傳遞,
-
物件在IPC間傳遞,
🔥 實作序列化的方式
-
實作Serializable介面
-
實作Parcelable介面
🔥 Serializable 和 Parcelable 區別
-
Serializable 是Java本身就支持的介面,
-
Parcelable 是Android特有的介面,效率比實作Serializable介面高效(可用于Intent資料傳遞,也可以用于行程間通信(IPC)),
-
Serializable的實作,只需要implements Serializable即可,這只是給物件打了一個標記,系統會自動將其序列化,
-
Parcelabel的實作,不僅需要implements Parcelabel,還需要在類中添加一個靜態成員變數CREATOR,這個變數需要實作 Parcelable.Creator介面,
-
Serializable 使用I/O讀寫存盤在硬碟上,而Parcelable是直接在記憶體中讀寫,
-
Serializable會使用反射,序列化和反序列化程序需要大量I/O操作,Parcelable 自己實作封送和解封( marshalled &unmarshalled)操作不需要用反射,資料也存放在Native記憶體中,效率要快很多
💥 實作Serializable
import java.io.Serializable;
public class UserSerializable implements Serializable {
public String name;
public int age;
}
然后你會發現沒有serialVersionUID,
Android Studio 是默認關閉 serialVersionUID 生成提示的,我們需要打開Setting,執行如下操作:

再次回到UserSerializable類,有個提示,就可以添加serialVersionUID了,

效果如下:
public class UserSerializable implements Serializable {
private static final long serialVersionUID = 1522126340746830861L;
public String name;
public int age = 0;
}
💥 實作Parcelable
Parcelabel的實作,不僅需要實作Parcelabel介面,還需要在類中添加一個靜態成員變數CREATOR,這個變數需要實作 Parcelable.Creator 介面,并實作讀寫的抽象方法,如下:
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
此時Android Studio 給我們了一個插件可自動生成Parcelable ,
🔥 自動生成 Parcelable
public class User {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
想進行序列化,但是自己寫太麻煩了,這里介紹個插件操作簡單易上手,
💥 先下載

💥 使用



💥 效果
public class User implements Parcelable {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeInt(this.age);
}
public void readFromParcel(Parcel source) {
this.name = source.readString();
this.age = source.readInt();
}
public User() {
}
protected User(Parcel in) {
this.name = in.readString();
this.age = in.readInt();
}
public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
return new User(source);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
}
搞定,
寫完了咱就運行走一波,
🔥 使用實體
💥 Serializable
MainActivity.class
Bundle bundle = new Bundle();
UserSerializable userSerializable=new UserSerializable("SCC",15);
bundle.putSerializable("user",userSerializable);
Intent intent = new Intent(MainActivity.this, BunderActivity.class);
intent.putExtra("user",bundle);
startActivity(intent);
BunderActivity.class
Bundle bundle = getIntent().getBundleExtra("user");
UserSerializable userSerializable= (UserSerializable) bundle.getSerializable("user");
MLog.e("Serializable:"+userSerializable.name+userSerializable.age);
日志:
2021-10-25 E/-SCC-: Serializable:SCC15
💥 Parcelable
MainActivity.class
Bundle bundle = new Bundle();
bundle.putParcelable("user",new UserParcelable("SCC",15));
Intent intent = new Intent(MainActivity.this, BunderActivity.class);
intent.putExtra("user",bundle);
startActivity(intent);
BunderActivity.class
Bundle bundle = getIntent().getBundleExtra("user");
UserParcelable userParcelable= (UserParcelable) bundle.getParcelable("user");
MLog.e("Parcelable:"+userParcelable.getName()+userParcelable.getAge());
日志:
2021-10-25 E/-SCC-: Parcelable:SCC15
🔥 Parcelable 中使用泛型
💥 UserParcelable
public class UserParcelable<T extends Parcelable> implements Parcelable {
private String name;
private int age;
private T data;
//...set/get部分省略
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public UserParcelable(String name, int age, T data) {
this.name = name;
this.age = age;
this.data = data;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeInt(this.age);
//這里:先保存這個泛型的類名
dest.writeString(data.getClass().getName());
dest.writeParcelable(this.data, flags);
}
public void readFromParcel(Parcel source) {
this.name = source.readString();
this.age = source.readInt();
//這里
String dataName = source.readString();
try {
this.data = source.readParcelable(Class.forName(dataName).getClassLoader());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
protected UserParcelable(Parcel in) {
this.name = in.readString();
this.age = in.readInt();
//這里
String dataName = in.readString();
try {
this.data = in.readParcelable(Class.forName(dataName).getClassLoader());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static final Creator<UserParcelable> CREATOR = new Creator<UserParcelable>() {
@Override
public UserParcelable createFromParcel(Parcel source) {
return new UserParcelable(source);
}
@Override
public UserParcelable[] newArray(int size) {
return new UserParcelable[size];
}
};
}
💥 Tman
public class Tman implements Parcelable {
String color;
public Tman(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.color);
}
public void readFromParcel(Parcel source) {
this.color = source.readString();
}
protected Tman(Parcel in) {
this.color = in.readString();
}
public static final Creator<Tman> CREATOR = new Creator<Tman>() {
@Override
public Tman createFromParcel(Parcel source) {
return new Tman(source);
}
@Override
public Tman[] newArray(int size) {
return new Tman[size];
}
};
}
💥 使用
MainActivity.class
Bundle bundle = new Bundle();
bundle.putParcelable("user",new UserParcelable<>("你好",2021,new Tman("紅色")));
Intent intent = new Intent(MainActivity.this, BunderActivity.class);
intent.putExtra("user",bundle);
startActivity(intent);
BunderActivity.class
Bundle bundle = getIntent().getBundleExtra("user");
UserParcelable<Tman> userParcelable= (UserParcelable) bundle.getParcelable("user");
MLog.e("Parcelable:"+userParcelable.getName()+userParcelable.getAge()+userParcelable.getData().getColor());
日志:
2021-10-25 E/-SCC-: Parcelable:你好2021紅色
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/340468.html
標籤:其他
