天天看点

Android 用Intent 传递对象之 Parcel

Android 为对象序列化开发了 Parcelable 接口,这个接口要比之前说的 Serializable 更为高效。使用会稍微复杂些。

其主要思想就是把 对象需要保存的数据 放在 parcel 对象中,然后再从 parcel 中恢复这个对象。可见实现 Parcelable这个接口的类,至少会实现两个方法一个用来 将数据保存进 parcel 对象,一个 方法用来从 parcel 中恢复对象。

示例结构:

Android 用Intent 传递对象之 Parcel

 先写一个实现了 Parcelable 接口的类 Person,可见 writeToParcel 用来向Parcel 对象写入要保存的对象,

CREATOR 用来恢复对象。存入的数据要和读取的数据顺序对应。

package com.serializabletest;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

class Person implements Parcelable {

  private static final String TAG = "Person";
  int ID;
  String Name;
  int age;

  public Person(int ID, String Name, int age) {
    this.ID = ID;
    this.Name = Name;
    this.age = age;
  }

  public void printPerson() {
    Log.d(TAG, "ID:" + ID);
    Log.d(TAG, "Name:" + Name);
    Log.d(TAG, "age:" + age);
  }

  // -------------------------------------------------
  public Person(Parcel in) {
    this.ID = in.readInt();
    this.Name = in.readString();
    this.age = in.readInt();
  }

  // 从 Parcel 对象 中恢复 Person 对象
  <span style="color:#ff0000;"><strong>public static final Parcelable.Creator<Person> CREATOR</strong> </span>= new Parcelable.Creator<Person>() {

    @Override
    public Person createFromParcel(Parcel arg0) {
      // TODO Auto-generated method stub
      return new Person(arg0);
    }

    @Override
    public Person[] newArray(int size) {
      // TODO Auto-generated method stub
      return new Person[size];
    }
  };

  /**
   * Describe the kinds of special objects contained in this Parcelable's
   * marshalled(排列) representation(表现).
   * 
   * 用来描述序列化对象中特殊对象的分布状况。
   * 
   * Returns a bitmask indicating the set of special object types marshalled
   * by the Parcelable.
   * 
   * @see android.os.Parcelable#describeContents()
   */

  @Override
  public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
  }

  /**
   * 用来将对象转换成 parcel 对象
   * 
   * 决定了把什么数据写入 parcel
   * 
   * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
   */
  // 把 Person 对象转化为 Parcel 对象存储起来
  @Override
  public void <strong><span style="color:#ff0000;">writeToParcel</span></strong>(Parcel arg0, int arg1) {
    // TODO Auto-generated method stub

    arg0.writeInt(ID);
    arg0.writeString(Name);
    arg0.writeInt(age);
  }
}      

MainActivity.java

package com.serializabletest;

import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;

public class MainActivity extends Activity {

  public static final String TAG = "MainActivity";
  /** 序列化测试
   * @see android.app.Activity#onCreate(android.os.Bundle)
   */
  private final String objName = "/mnt/sdcard/object.txt";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("Intent 传递对象");
    setContentView(R.layout.activity_main);

  }

  public void onBtnClick(View view) throws IOException {
    Person person = new Person(1, "张三", 26);

    Bundle bundle = new Bundle();
    bundle.putParcelable("person", person);
    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    intent.putExtras(bundle);
    startActivity(intent);
  }
}      

SecondActivity.java

package com.serializabletest;

import java.io.IOException;
import java.io.StreamCorruptedException;

import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.content.Intent;

public class SecondActivity extends Activity {

  public static final String TAG = "MainActivity";
  /** 序列化测试
   * @see android.app.Activity#onCreate(android.os.Bundle)
   */
  private final String objName = "/mnt/sdcard/object.txt";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("Intent 传递对象");
    setContentView(R.layout.activity_second);

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    Person person = bundle.getParcelable("person");
    person.printPerson();
  }

  public void onBtnClick(View view) throws StreamCorruptedException,
      IOException, ClassNotFoundException {
    this.finish();
  }

}