github连接:https://github.com/lixiang0/DemoLibs
在实际的使用中,我们经常需要在不同的进程中传递对象实例,但是却不能直接的传递对象,因而需要通过对象的序列化和反序列化来实现对对象的传递。序列化之后标志着我们可以直接传递对象了。
下面我们讲实现通过一个例子来利用Parcelable接口实现对象的序列化。通过在MainActivity中按钮的点击将一个User对象传递到MainActivity2并显示出来。
1.构造User类,并实现Parcelable接口
package string.pub.serial.entity;
import android.os.Parcel;
import android.os.Parcelable;
public class User implements Parcelable{
String name="ruben";
int age=26;
String sex="man";
String addr="shenzhen";
String describe="a coder";
public User() {
// TODO Auto-generated constructor stub
}
public User(Parcel paramParcel) {
// TODO Auto-generated constructor stub
name=paramParcel.readString();
age=paramParcel.readInt();
sex=paramParcel.readString();
addr=paramParcel.readString();
describe=paramParcel.readString();
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", sex=" + sex + ", addr=" + addr + ", describe=" + describe
+ "]";
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel paramParcel, int paramInt) {
// TODO Auto-generated method stub
paramParcel.writeString(name);
paramParcel.writeInt(age);
paramParcel.writeString(sex);
paramParcel.writeString(addr);
paramParcel.writeString(describe);
}
public static final Parcelable.Creator<User> CREATOR=new Creator<User>() {
@Override
public User[] newArray(int paramInt) {
// TODO Auto-generated method stub
return new User[paramInt];
}
@Override
public User createFromParcel(Parcel paramParcel) {
// TODO Auto-generated method stub
return new User(paramParcel);
}
};
}
2.实现MainActivity:
package string.pub.serial;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import string.pub.serial.entity.User;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
StringBuilder sb=new StringBuilder();
Button btn_click=new Button(this);
btn_click.setText("click to MainActivity2");
btn_click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(MainActivity.this, MainActivity2.class);
intent.putExtra("value", new User());
startActivity(intent);
}
});
ll.addView(btn_click);
setContentView(ll);
}
}
3.实现MainActivity2:
package string.pub.serial;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import string.pub.serial.entity.User;
public class MainActivity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv_content=new TextView(this);
User user=(User)getIntent().getParcelableExtra("value");
tv_content.setText(user.toString());
ll.addView(tv_content);
setContentView(ll);
}
}
4.注册清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="string.pub.serial"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity2"
android:label="@string/app_name" >
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
5.实现效果:
总结:Parcelable接口是Android中的序列化与反序列化的实现方案,它可以实现在不同进程中进行对象的传输和持久化。