上一篇中我们透过源码看到了Parcel背后的机制,本质上把它当成一个Serialize就可以了,只是它是在内存中完成的序列化和反序列化,利用的是连续的内存空间,因此会更加高效。
我们接下来要说的是Parcel类如何应用。就应用程序而言,最常见使用Parcel类的场景就是在Activity间传递数据。没错,在Activity间使用Intent传递数据的时候,可以通过Parcelable机制传递复杂的对象。
下面来看一下简单的代码:
UserInfo类,实现了Parcelable接口:
package com.demo.entity; import java.util.ArrayList;
import java.util.HashMap; import android.os.Parcel;
import android.os.Parcelable; /**
* 用户信息,实现Parcelable接口
* String,int,String[],ArrayList<HashMap<String,Object>>这四种类型测试
* @author weijiang204321
*
*/
public class UserInfo implements Parcelable{ private String id;
private String name;
private int age;
private String[] boyfriendAry = new String[3];
private ArrayList<HashMap<String,SonInfo>> listData = new ArrayList<HashMap<String,SonInfo>>();
private SonInfo sonInfo; public UserInfo(){
} public UserInfo(String id,String name,int age,String[] boyfriendAry,ArrayList<HashMap<String,SonInfo>> listData){
this.id = id;
this.name = name;
this.age = age;
this.boyfriendAry = boyfriendAry;
this.listData = listData;
} public UserInfo(Parcel source){
id = source.readString();
name = source.readString();
age = source.readInt();
sonInfo = source.readParcelable(SonInfo.class.getClassLoader());
source.readStringArray(boyfriendAry);
source.readList(listData, SonInfo.class.getClassLoader());
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} 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 SonInfo getSonInfo(){
return sonInfo;
} public void setSonInfo(SonInfo sonInfo){
this.sonInfo = sonInfo;
} public String[] getBoyfriendAry() {
return boyfriendAry;
} public void setBoyfriendAry(String[] boyfriendAry) {
this.boyfriendAry = boyfriendAry;
} public ArrayList<HashMap<String, SonInfo>> getListData() {
return listData;
} public void setListData(ArrayList<HashMap<String, SonInfo>> listData) {
this.listData = listData;
} @Override
public int describeContents() {
return 0;
} //进行序列化存储
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(name);
dest.writeInt(age);
dest.writeParcelable(sonInfo, Parcelable.PARCELABLE_WRITE_RETURN_VALUE); dest.writeStringArray(boyfriendAry);
dest.writeList(listData);
} public static final Parcelable.Creator<UserInfo> CREATOR = new Creator<UserInfo>() {
@Override
public UserInfo[] newArray(int size) {
return new UserInfo[size];
} //将Parcel对象反序列化为UserInfo
@Override
public UserInfo createFromParcel(Parcel source) {
return new UserInfo(source);
}
}; }
同样SonInfo类也要实现Parcelable接口:
package com.demo.entity; import android.os.Parcel;
import android.os.Parcelable; public class SonInfo implements Parcelable{ private String id;
private String name; public SonInfo(){ } public SonInfo(String id,String name){
this.id = id;
this.name = name;
} public SonInfo(Parcel source){
this.id = source.readString();
this.name = source.readString();
} public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} @Override
public int describeContents() {
return 0;
} @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(name);
} public static final Parcelable.Creator<SonInfo> CREATOR = new Creator<SonInfo>() {
@Override
public SonInfo[] newArray(int size) {
return new SonInfo[size];
} //将Parcel对象反序列化为UserInfo
@Override
public SonInfo createFromParcel(Parcel source) {
return new SonInfo(source);
}
}; }
下面来看一下测试类:
MainActivity类,在这里面进行数据的封装,然后传递到StubActivity类中:
package com.demo.parcel; import java.util.ArrayList;
import java.util.HashMap; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle; import com.demo.entity.SonInfo;
import com.demo.entity.UserInfo; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Intent intent = new Intent(this,StubActivity.class);
ArrayList<HashMap<String,SonInfo>> listData = new ArrayList<HashMap<String,SonInfo>>();
HashMap<String,SonInfo> tempMap = new HashMap<String,SonInfo>();
tempMap.put("one_child", new SonInfo("001_1","jiangwei_one_child"));
listData.add(tempMap); UserInfo userInfo = new UserInfo("001","jiangwei",24,new String[]{"张三","李四","王五"},listData);
SonInfo sonInfo = new SonInfo();
sonInfo.setId("001_1");
sonInfo.setName("jiangwei_child");
userInfo.setSonInfo(sonInfo);
intent.putExtra("userinfo", userInfo);
startActivity(intent); } }
下面就是StubActivity类,在这个类中将读出数据进行显示:
package com.demo.parcel; import java.util.ArrayList;
import java.util.HashMap; import android.app.Activity;
import android.os.Bundle;
import android.util.Log; import com.demo.entity.SonInfo;
import com.demo.entity.UserInfo; public class StubActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stub); UserInfo userInfo = (UserInfo)this.getIntent().getParcelableExtra("userinfo");
SonInfo sonInfo = userInfo.getSonInfo(); String[] boyfriend = userInfo.getBoyfriendAry();
for(int i=0;i<boyfriend.length;i++){
Log.e("boyfriend_name:",boyfriend[i]);
} ArrayList<HashMap<String,SonInfo>> listData = userInfo.getListData();
for(int j=0;j<listData.size();j++){
SonInfo temp_soninfo = listData.get(j).get("one_child");
Log.e("OneChildId:",temp_soninfo.getId()+"");
Log.e("OneChildName:",temp_soninfo.getName()+"");
} Log.e("UserId:",userInfo.getId()+"");
Log.e("UserName:",userInfo.getName()+"");
Log.e("UserAge:",userInfo.getAge()+"");
Log.e("Son_id:",sonInfo.getId()+"");
Log.e("Son_name:",sonInfo.getName()+""); } }
运行结果:
这样就成功了使用了Parcelable进行数据的传输