android序列化
阿新 • • 發佈:2018-12-24
activity 之間通過 Intent 來傳遞資料, Intent 傳遞的資料能只 能是一些基本的資料型別、字串以及對應的陣列。但是,加入現在要傳遞一個自定義的java物件,怎麼辦? 這裡需要物件序列化。
序列化是指將一個物件的屬性和方法轉化為一種序列化的格式,也就是把這個java物件轉換為位元組序列,以便儲存在檔案中或網路上傳輸。
序列化一般用於以下場景:
永久性儲存物件,儲存物件的位元組序列到本地檔案中;
通過序列化物件在網路中傳遞物件;
通過序列化在程序間傳遞物件。
Serializable
Student類實現Serializable
public class Student implements Serializable{
private String name;
private int age;
public Student(){
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
MainActivity.java
public class MainActivity extends Activity {
EditText e1;
EditText e2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText) findViewById(R.id.editText1);
e2=(EditText) findViewById(R.id.editText2);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name=e1.getText().toString();
int age=Integer.valueOf(e2.getText().toString());
Student st=new Student(name,age);
Intent intent=new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
//設定Bundle物件
Bundle bundle =new Bundle();
//bundle相當於容器存取序列化物件
bundle.putSerializable("student",st);
//把Bundle物件新增到intent上面
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
SecondActivity.java
public class SecondActivity extends Activity {
TextView t1;
TextView t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
t1=(TextView) findViewById(R.id.textView1);
t2=(TextView) findViewById(R.id.textView2);
Intent intent=getIntent();
//獲取Bundld物件
Bundle bundle=intent.getExtras();
Student stu=(Student)bundle.getSerializable("student");
String name=stu.getName();
int age=stu.getAge();
t1.setText(name);
t2.setText(String.valueOf(age));
}
}
Parcelable
實現 Parcelable 介面稍微複雜一些, 通過實現 Parcelable 介面序列化物件 的步驟:
- implements Parcelable 介面;
- 重寫介面中的writeToParcel方法,將你的物件序列化為一個Parcel物件
- 重寫describleContents 方法,意識一個內容介面描述,預設返回0就可以
- 例項化靜態內部物件 Creator實現介面Parcelable.Creator;
- 通過writeToParcel 將你的物件對映成Parcel物件,在通過createFromParcel將Parcel物件對映成你的物件。
也可以將Parcel看成是一個流,通過writeToParcecl把物件寫到流裡面,在通過createFromParcel從流裡讀取物件,只不過這個過程需要你來實現,因此寫的順序和讀的順序必須一致。
Book.java
public class Book implements Parcelable{
String name;
String msg;
public Book(String name,String msg){
//第一次
Log.d("Tag", "Book(String name,String msg)");
this.name=name;
this.msg=msg;
}
public String getName(){
return name;
}
public String getMsg(){
return msg;
}
@Override
public int describeContents() {
//沒有執行
Log.d("Tag", "describeContents()");
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
//第二個寫資料
Log.d("Tag", "writeToParcel()");
dest.writeString(name);
dest.writeString(msg);
}
public static final Parcelable.Creator<Book> CREATOR=new Creator<Book>() {
@Override
public Book[] newArray(int size) {
//沒有執行
Log.d("Tag", " Book[] newArray(int size)");
//改寫返回值,注意格式
return new Book[size];
}
@Override
public Book createFromParcel(Parcel source) {
//第三個建立內部類物件
Log.d("Tag", "Book createFromParcel(Parcel source)");
//改寫返回值,注意格式
return new Book(source);
}
};
public Book(Parcel source) {
//第四 讀資料
Log.d("Tag", "Book(Parcel source)");
//讀資料,注意與寫資料的順序一致
name=source.readString();
msg=source.readString();
}
}
MainActivity.java
public class MainActivity extends Activity {
EditText e1;
EditText e2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText) findViewById(R.id.editText1);
e2=(EditText) findViewById(R.id.editText2);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
//設定Bundle物件
Bundle bundle =new Bundle();
String name=e1.getText().toString();
String msg=e2.getText().toString();
Log.d("Tag", "MainActivity0");
Book book=new Book(name, msg);
Log.d("Tag", "MainActivity1");
bundle.putParcelable("book", book);
Log.d("Tag", "MainActivity2");
intent.putExtras(bundle);
Log.d("Tag", "MainActivity3");
startActivity(intent);
Log.d("Tag", "MainActivity4");
}
});
}
}
SecondActivity.java
public class SecondActivity extends Activity {
public class SecondActivity extends Activity {
TextView t1;
TextView t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Log.d("Tag", "onCreate方法1");
t1=(TextView) findViewById(R.id.textView1);
t2=(TextView) findViewById(R.id.textView2);
Intent intent=getIntent();
Log.d("Tag", "onCreate方法2");
//獲取Bundld物件
Bundle bundle=intent.getExtras();
Log.d("Tag", "onCreate方法3");
Book book=bundle.getParcelable("book");
Log.d("Tag", "onCreate方法4");
String name=book.getName();
String msg=book.getMsg();
t1.setText(name);
t2.setText(msg);
}
}
結果: