一、概述

Android中Intent传递对象有两种方式,这两个对象分别实现了Parcelable、Serializable两个接口。

bundle.putSerializable(key, value)
bundle.putParcelable(key, value);

1.Serializable

特点:Serializable是JavaSe的接口,在序列化的时候会产生大量的临时变量,从而容易引起频繁的GC,性能比较低下。

用法实例:

public class Dog implements Serializable {    private static final long serialVersionUID = 1l ;    private String name ;    private int age ;    public Dog(String name, int age) {        super();        this.name = name;        this.age = 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 static void main(String[] args) {        Dog dog = new Dog("小黄",1) ;        try {            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("foo.ser")));            oos.writeObject(dog) ;            System.out.println("----------------------");            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("foo.ser")));            Dog d = (Dog) ois.readObject();            System.out.println("name:" + dog.getName() + ":" + dog.getAge() );        }  catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }

注意一些serialVersionUID:Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常。(InvalidCastException)

2.Parcelable

特点:这是Android中实现序列化的方式,使用起来比Serializable要麻烦点,效率比Serializable较高。使用Parcelable步骤如下:

1.继承Parcelable接口,实现describeContents、writeToParcel两个方法。

@Override    public int describeContents() {        return 0;    }
@Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(name);        dest.writeInt(age) ;    }

2.创建静态变量CREATOR,并且实现接口方法

例如:

public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {        @Override        public Person createFromParcel(Parcel source) {// Perseon p = new Perseon();// p.setAge(source.readInt()) ;// p.setName(source.readString());            return new Person(source);        }        @Override        public Person[] newArray(int size) {            return new Person[size];        }    };

注意:这里的CREATOR 的修饰符必须是public static final。

3.就Parcelable做一个Demo

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="${relativePackage}.${activityClass}" >        <TextView             android:id="@+id/tv_name"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Mrs.Lee"            />         <TextView             android:id="@+id/tv_age"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="21"            />    <Button        android:id="@+id/click"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="start" /></LinearLayout>

主Activity:

public class MainActivity extends Activity {    private String name;    private String age;    private TextView tv_name;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv_name = (TextView) findViewById(R.id.tv_name);        TextView tv_age = (TextView) findViewById(R.id.tv_age);        name = (String) tv_name.getText();        age = (String) tv_age.getText();        findViewById(R.id.click).setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                Person p = new Person(name,Integer.parseInt(age)) ;                Bundle b = new Bundle() ;                b.putParcelable("msg", p);                Intent intent = new Intent(MainActivity.this,SecondActivity.class);                intent.putExtras(b);                startActivity(intent);            }        });    }}

第二个Activity:

public class SecondActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        Intent intent = getIntent();        Bundle extras = intent.getExtras();        Person p = intent.getParcelableExtra("msg");        int age = p.getAge();        String name = p.getName();        Toast.makeText(this, name + ":" + age ,  1).show() ;    }}

运行结果如下:

06-17 23:02:34.303: I/System.out(12956): Mrs.Lee:21

源码下载

更多相关文章

  1. 走进Java Android(安卓)的线程世界(三)Hander消息机制
  2. android 应用程序4种编译方法
  3. 数据存储之SharedPreferences存储——第一行代码Android学习笔记
  4. android用户界面-组件Widget-网络视图WebView
  5. Android(安卓)AlertDialog 方法setView(view,0,0,0,0)开发自定义
  6. Android(安卓)进阶——Framework 核心四大组件之跨进程共享组件C
  7. AOP编程_Android优雅权限框架(2)Demo完全解析
  8. Android手势滑动实现ImageView缩放图片大小
  9. TextView自定义跑马灯效果

随机推荐

  1. 自定义ListView背景(解决了拖动变黑的效果
  2. HTML5移动Web应用程序的JavaScript 框架
  3. Android Studio 出现 Gradle's dependenc
  4. 深度揭秘android摄像头的autoFocus-----
  5. Android平台架构简介
  6. Android开发中的autocomplete控件
  7. 开始学习android
  8. windows平台下Android studio开发环境搭
  9. Android中Preference的使用以及监听事件
  10. Android触摸事件的分发处理