import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
public class Test extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = getIntent();
Person p = i.getParcelableExtra("yes");
System.out.println("---->"+p.name);
System.out.println("---->"+p.map.size());
}
}
发送的Activity
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TestNew extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
Person p = new Person();
p.map = new HashMap<String,String>();
p.map.put("yes", "ido");
p.name="ok";
intent.putExtra("yes", p);
intent.setClass(this, Test.class);
startActivity(intent);
}
}
Parcelable的实现类
import java.util.HashMap;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
public HashMap<String,String> map = new HashMap<String,String> ();
public String name ;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeMap(map);
dest.writeString(name);
}
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
//重写Creator
@Override
public Person createFromParcel(Parcel source) {
Person p = new Person();
p.map=source.readHashMap(HashMap.class.getClassLoader());
p.name=source.readString();
return p;
}
@Override
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return null;
}
};
}
下面写一下转载自http://blog.csdn.net/zyc13701469860/article/details/6429934的对Parcel的解说:
在SDK中 Parcelable类的概述是这样的:Interface for classes whose instances can be written to and restored from a Parcel
. Classes implementing the Parcelable interface must also have a static field called CREATOR
, which is an object implementing theParcelable.Creator
interface.
这个接口的实例是通过Parcel进行存储的,在使用Parcelable的时候必须使用Parcelable.Creator。
下面上代码:
首先修改main.xml,增加一个button
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Parcelable"> </Button> </LinearLayout>
然后是主Activity ParcelableTest.java,这个类显示UI,并通过button点击事件启动另一个Activity -- ParcelableTest2,同时通过Parcelable接口传递一些数据。
package parcelable_test.com; 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; public class ParcelableTest extends Activity implements OnClickListener{ public static final String KEY = "key"; private Button button; public static final String TAG = "Parcelable"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); Log.d(TAG, "ParcelableTest"); } private void init(){ button = (Button)findViewById(R.id.button); button.setOnClickListener(this); } private void fun(){ Log.d(TAG, "fun"); Person mPerson = new Person(); mPerson.setName("tom"); mPerson.setAge(25); Intent mIntent = new Intent(this,parcelable_test.com.ParcelableTest2.class); Bundle mBundle = new Bundle(); mBundle.putParcelable(KEY, mPerson); mIntent.putExtras(mBundle); startActivity(mIntent); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: fun(); break; default: break; } } }
ParcelableTest2.java,这个类用于获取ParcelableTest传出的数据,并显示在UI上。
package parcelable_test.com; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class ParcelableTest2 extends Activity{ private static final String TAG = ParcelableTest.TAG; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.d(TAG, "ParcelableTest2"); TextView textView = new TextView(this); Person mPerson = (Person)getIntent().getParcelableExtra(ParcelableTest.KEY); textView.setText("name = " + mPerson.getName() + " age = " + mPerson.getAge()); setContentView(textView); } }
下面就是最重要的类Person,Person类引用了Parcelable接口
package parcelable_test.com; 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 = ParcelableTest.TAG; 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 final Parcelable.Creator<Person> CREATOR = new Creator<Person>() { @Override public Person createFromParcel(Parcel source) { Log.d(TAG,"createFromParcel"); Person mPerson = new Person(); mPerson.name = source.readString(); mPerson.age = source.readInt(); return mPerson; } @Override public Person[] newArray(int size) { // TODO Auto-generated method stub return new Person[size]; } }; @Override public int describeContents() { // TODO Auto-generated method stub Log.d(TAG,"describeContents"); return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub Log.d(TAG,"writeToParcel"); dest.writeString(name); dest.writeInt(age); } }
通过log可知程序的运行情况,在mBundle.putParcelable(KEY, mPerson);时,调用了Person类中的public void writeToParcel(Parcel dest, int flags)方法,并向dest写数据,在 Person mPerson = (Person)getIntent().getParcelableExtra(ParcelableTest.KEY);的时候,调用了Person类中的public Person createFromParcel(Parcel source) 方法,创建了一个Person对象,并给这个对象的属性赋值,这里的Parcel source和Parcel dest,是相同的,然后返回这个Person对象。最后就可以打印出mPerson的属性信息了。
Parcel可以用来存放很多数据,比如都是String和int,又或者有一个String和HashMap<String,String>的集合,都很方便。