android 对象序列化
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Android 对象序列化Parcelable 使用实例:
1.发送数据类:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import .Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class A extends Activity {
public A(){}
@Override
public void onDestroy()
{
}
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("go actB");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Person mPerson = new Person();
mPerson.setName("tom");
mPerson.setAge(25);
Intent mIntent = new Intent(A.this, B.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable("key", mPerson);
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
});
LinearLayout layout = new LinearLayout(this);
layout.addView(exitBtn);
this.setContentView(layout);
}
}
2.接收数据类:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class B extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
Person mPerson = (Person)getIntent().getParcelableExtra("key");
Log.d("param ","***name = " + mPerson.getName() + " age = " +
mPerson.getAge());
textView.setText("name = " + mPerson.getName() + " age = " +
mPerson.getAge());
layout.addView(textView);
this.setContentView(layout);
}
}
3.实例化对象类:
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
public class Person implements Parcelable
{
private String name;
private int age;
private static final String TAG = "Parcelable";
public String getName()
{
return name;
}
public void setName(String name)
{
= name;
}
public int getAge()
{