一.先从Serialize说起
我们都知道JAVA中的Serialize机制,译成串行化、序列化……,其作用是能将数据对象存入字节流当中,在需要时重新生成对象。主要应用是利用外部存储设备保存对象状态,以及通过网络传输对象等。
二.Android中的新的序列化机制
在Android系统中,定位为针对内存受限的设备,因此对性能要求更高,另外系统中采用了新的IPC(进程间通信)机制,必然要求使用性能更出色的对象传输方式。在这样的环境下,Parcel被设计出来,其定位就是轻量级的高效的对象序列化和反序列化机制。
三.Parcel类的背后
在Framework中有parcel类,源码路径是:
Frameworks/base/core/java/android/os/Parcel.java
典型的源码片断如下:
- /**
- * Write an integer value into the parcel at the current dataPosition(),
- * growing dataCapacity() if needed.
- */
- public final native void writeInt(int val);
- /**
- * Write a long integer value into the parcel at the current dataPosition(),
- * growing dataCapacity() if needed.
- */
- public final native void writeLong(long val);
从中我们看到,从这个源程序文件中我们看不到真正的功能是如何实现的,必须透过JNI往下走了。于是,Frameworks/base/core/jni/android_util_Binder.cpp中找到了线索
- static void android_os_Parcel_writeInt(JNIEnv* env, jobject clazz, jint val)
- {
- Parcel* parcel = parcelForJavaObject(env, clazz);
- if (parcel != NULL) {
- const status_t err = parcel->writeInt32(val);
- if (err != NO_ERROR) {
- jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
- }
- }
- }
- static void android_os_Parcel_writeLong(JNIEnv* env, jobject clazz, jlong val)
- {
- Parcel* parcel = parcelForJavaObject(env, clazz);
- if (parcel != NULL) {
- const status_t err = parcel->writeInt64(val);
- if (err != NO_ERROR) {
- jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
- }
- }
- }
从这里我们可以得到的信息是函数的实现依赖于Parcel指针,因此还需要找到Parcel的类定义,注意,这里的类已经是用C++语言实现的了。
找到Frameworks/base/include/binder/parcel.h和Frameworks/base/libs/binder/parcel.cpp。终于找到了最终的实现代码了。
有兴趣的朋友可以自己读一下,不难理解,这里把基本的思路总结一下:
1. 整个读写全是在内存中进行,主要是通过malloc()、realloc()、memcpy()等内存操作进行,所以效率比JAVA序列化中使用外部存储器会高很多;
2. 读写时是4字节对齐的,可以看到#define PAD_SIZE(s) (((s)+3)&~3)这句宏定义就是在做这件事情;
3. 如果预分配的空间不够时newSize = ((mDataSize+len)*3)/2;会一次多分配50%;
4. 对于普通数据,使用的是mData内存地址,对于IBinder类型的数据以及FileDescriptor使用的是mObjects内存地址。后者是通过flatten_binder()和unflatten_binder()实现的,目的是反序列化时读出的对象就是原对象而不用重新new一个新对象。
好了,这就是Parcel背后的动作,全是在一块内存里进行读写操作,就不啰嗦了,把parcel的代码贴在这供没有源码的朋友参考吧。接下来我会用一个小DEMO演示一下Parcel类在应用程序中的使用,详见《探索Android中的Parcel机制(下)》。
上一篇中我们透过源码看到了Parcel背后的机制,本质上把它当成一个Serialize就可以了,只是它是在内存中完成的序列化和反序列化,利用的是连续的内存空间,因此会更加高效。
我们接下来要说的是Parcel类如何应用。就应用程序而言,最常见使用Parcel类的场景就是在Activity间传递数据。没错,在Activity间使用Intent传递数据的时候,可以通过Parcelable机制传递复杂的对象。
在下面的程序中,MyColor用于保存一个颜色值,MainActivity在用户点击屏幕时将MyColor对象设成红色,传递到SubActivity中,此时SubActivity的TextView显示为红色的背景;当点击SubActivity时,将颜色值改为绿色,返回MainActivity,期望的是MainActivity的TextView显示绿色背景。
来看一下MyColor类的实现代码:
- package com.wenbin.test;
- import android.graphics.Color;
- import android.os.Parcel;
- import android.os.Parcelable;
- /**
- * @author 曹文斌
- * http://blog.youkuaiyun.com/caowenbin
- *
- */
- public class MyColor implements Parcelable {
- private int color=Color.BLACK;
- MyColor(){
- color=Color.BLACK;
- }
- MyColor(Parcel in){
- color=in.readInt();
- }
- public int getColor(){
- return color;
- }
- public void setColor(int color){
- this.color=color;
- }
- @Override
- public int describeContents() {
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- dest.writeInt(color);
- }
- public static final Parcelable.Creator<MyColor> CREATOR
- = new Parcelable.Creator<MyColor>() {
- public MyColor createFromParcel(Parcel in) {
- return new MyColor(in);
- }
- public MyColor[] newArray(int size) {
- return new MyColor[size];
- }
- };
- }
该类实现了Parcelable接口,提供了默认的构造函数,同时也提供了可从Parcel对象开始的构造函数,另外还实现了一个static的构造器用于构造对象和数组。代码很简单,不一一解释了。
再看MainActivity的代码:
- package com.wenbin.test;
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.MotionEvent;
- /**
- * @author 曹文斌
- * http://blog.youkuaiyun.com/caowenbin
- *
- */
- public class MainActivity extends Activity {
- private final int SUB_ACTIVITY=0;
- private MyColor color=new MyColor();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode==SUB_ACTIVITY){
- if (resultCode==RESULT_OK){
- if (data.hasExtra("MyColor")){
- color=data.getParcelableExtra("MyColor"); //Notice
- findViewById(R.id.text).setBackgroundColor(color.getColor());
- }
- }
- }
- }
- @Override
- public boolean onTouchEvent(MotionEvent event){
- if (event.getAction()==MotionEvent.ACTION_UP){
- Intent intent=new Intent();
- intent.setClass(this, SubActivity.class);
- color.setColor(Color.RED);
- intent.putExtra("MyColor", color);
- startActivityForResult(intent,SUB_ACTIVITY);
- }
- return super.onTouchEvent(event);
- }
- }
下面是SubActivity的代码:
- package com.wenbin.test;
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.widget.TextView;
- /**
- * @author 曹文斌
- * http://blog.youkuaiyun.com/caowenbin
- *
- */
- public class SubActivity extends Activity {
- private MyColor color;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ((TextView)findViewById(R.id.text)).setText("SubActivity");
- Intent intent=getIntent();
- if (intent!=null){
- if (intent.hasExtra("MyColor")){
- color=intent.getParcelableExtra("MyColor");
- findViewById(R.id.text).setBackgroundColor(color.getColor());
- }
- }
- }
- @Override
- public boolean onTouchEvent(MotionEvent event){
- if (event.getAction()==MotionEvent.ACTION_UP){
- Intent intent=new Intent();
- if (color!=null){
- color.setColor(Color.GREEN);
- intent.putExtra("MyColor", color);
- }
- setResult(RESULT_OK,intent);
- finish();
- }
- return super.onTouchEvent(event);
- }
- }
下面是main.xml的代码:
- <?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:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- android:id="@+id/text"
- />
- </LinearLayout>
注意的是在MainActivity的onActivityResult()中,有一句color=data.getParcelableExtra("MyColor"),这说明的是反序列化后是一个新的MyColor对象,因此要想使用这个对象,我们做了这个赋值语句。
记得在上一篇《探索Android中的Parcel机制(上)》中提到,如果数据本身是IBinder类型,那么反序列化的结果就是原对象,而不是新建的对象,很显然,如果是这样的话,在反序列化后在MainActivity中就不再需要color=data.getParcelableExtra("MyColor")这句了。因此,换一种MyColor的实现方法,令其中的int color成员变量使用IBinder类型的成员变量来表示。
新建一个BinderData类继承自Binder,代码如下:
- package com.wenbin.test;
- import android.os.Binder;
- /**
- * @author 曹文斌
- * http://blog.youkuaiyun.com/caowenbin
- *
- */
- public class BinderData extends Binder {
- public int color;
- }
修改MyColor的代码如下:
- package com.wenbin.test;
- import android.graphics.Color;
- import android.os.Parcel;
- import android.os.Parcelable;
- /**
- * @author 曹文斌
- * http://blog.youkuaiyun.com/caowenbin
- *
- */
- public class MyColor implements Parcelable {
- private BinderData data=new BinderData();
- MyColor(){
- data.color=Color.BLACK;
- }
- MyColor(Parcel in){
- data=(BinderData) in.readValue(BinderData.class.getClassLoader());
- }
- public int getColor(){
- return data.color;
- }
- public void setColor(int color){
- data.color=color;
- }
- @Override
- public int describeContents() {
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- dest.writeValue(data);
- }
- public static final Parcelable.Creator<MyColor> CREATOR
- = new Parcelable.Creator<MyColor>() {
- public MyColor createFromParcel(Parcel in) {
- return new MyColor(in);
- }
- public MyColor[] newArray(int size) {
- return new MyColor[size];
- }
- };
- }
去掉MainActivity的onActivityResult()中的color=data.getParcelableExtra("MyColor")一句,变成:
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode==SUB_ACTIVITY){
- if (resultCode==RESULT_OK){
- if (data.hasExtra("MyColor")){
- findViewById(R.id.text).setBackgroundColor(color.getColor());
- }
- }
- }
- }
再次运行程序,结果符合预期。
以上就是Parcel在应用程序中的使用方法,与Serialize还是挺相似的,详细的资料当然还是要参考Android SDK的开发文档了。
——欢迎转载,请注明出处 http://blog.youkuaiyun.com/caowenbin ——