Android 中Parcelable的作用

android提供了一种新的类型:Parcel。本类被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。 除了基本类型以

外,只有实现了Parcelable接口的类才能被放入Parcel中。

 

Parcelable实现要点:需要实现三个东西

1)writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中.声明如下:

writeToParcel (Parcel dest, int flags) 具体参数含义见javadoc

2)describeContents方法。没搞懂有什么用,反正直接返回0也可以

3)静态的Parcelable.Creator接口,本接口有两个方法:

createFromParcel(Parcel in) 实现从in中创建出类的实例的功能

newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。估计本方法是供外部类反序列化本类数组使用。

测试用的接收信息Activity

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>的集合,都很方便。

 

 





你可能感兴趣的:(Android 中Parcelable的作用 )