Android中的Parcel机制
    实现了Bundle传递对象
    使用Bundle传递对象,首先要将其序列化,但是,在Android中要使用这种传递对象的方式需要用到Android Parcel机制,即,Android实现的轻量级的高效的对象序列化和反序列化机制。

JAVA中的Serialize机制,译成串行化、序列化……,其作用是能将数据对象存入字节流当中,在需要时重新生成对象。主要应用是利用外部存储设备保存对象状态,以及通过网络传输对象等。
    
    Android中的新的序列化机制
        在Android系统中,定位为针对内存受限的设备,因此对性能要求更高,另外系统中采用了新的IPC(进程间通信)机制,必然要求使用性能更出色的对象传输方式。在这样的环境下,
        Parcel被设计出来,其定位就是轻量级的高效的对象序列化和反序列化机制。
        Android中序列化有以下几个特征:
        1. 整个读写全是在内存中进行,所以效率比JAVA序列化中使用外部存储器会高很多;
        2. 读写时是4字节对齐的
        3. 如果预分配的空间不够时,会一次多分配50%;
        4. 对于普通数据,使用的是mData内存地址,对于IBinder类型的数据以及FileDescriptor使用的是mObjects内存地址。后者是通过flatten_binder()和unflatten_binder()实现的,目的是反序列化时读出的对象就是原对象而不用重新new一个新对象。

package com.parcel.main;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MyTestParcelActivity extends Activity { public final static int Sub_Main = ;
public final static int Sub_2_Main = ; private Button setSubMain;
private Button setSub2Main;
private TextView mTextView; private Drawable defDrawable;
private MyParcel mMyParcel = new MyParcel();
private BinderMyParcel mBinderMyParcel = new BinderMyParcel(); /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); initComponent();
} private void initComponent() {
setSubMain = (Button) findViewById(R.id.btn01);
if (setSubMain != null) {
setSubMain.setOnClickListener(setSubMainXML);
} setSub2Main = (Button) findViewById(R.id.btn04);
if (setSub2Main != null) {
setSub2Main.setOnClickListener(setSub2MainXML);
} mTextView = (TextView) findViewById(R.id.main_tv);
defDrawable = mTextView.getBackground();
} private View.OnClickListener setSubMainXML = new OnClickListener() { @Override
public void onClick(View v) {
Intent mIntent = new Intent();
if (mMyParcel != null) {
mMyParcel.setColor(Color.RED);
mIntent.putExtra("MyColor", mMyParcel);
}
mIntent.setClass(MyTestParcelActivity.this,
SubMyTestParcelActivity.class); mTextView.setBackgroundDrawable(defDrawable); startActivityForResult(mIntent, Sub_Main);
}
};
private View.OnClickListener setSub2MainXML = new OnClickListener() { @Override
public void onClick(View v) {
Intent intent = new Intent();
if (mBinderMyParcel != null) {
mBinderMyParcel.setColor(Color.MAGENTA);
intent.putExtra("BinderMyColor", mBinderMyParcel);
}
intent.setClass(MyTestParcelActivity.this,
Sub2MyTestParcelActivity.class); mTextView.setBackgroundDrawable(defDrawable); startActivityForResult(intent, Sub_2_Main);
}
}; @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == Sub_Main) {
if (data.hasExtra("MyColor")) {
/**
* color=data.getParcelableExtra("MyColor"),
* 这说明反序列化后是一个新的MyColor对象
*
* 如果数据本身是IBinder类型,那么反序列化的结果就是原对象,而不是新建的对象
*/
mMyParcel = data.getParcelableExtra("MyColor");
((TextView) findViewById(R.id.main_tv))
.setTextColor(mMyParcel.getColor());
}
} else if (requestCode == Sub_2_Main) {
if (data.hasExtra("BinderMyColor")) {
((TextView) findViewById(R.id.main_tv))
.setBackgroundColor(mBinderMyParcel.getColor());
}
}
}
} @Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
} }

使用非binder类型代码

package com.parcel.main;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class SubMyTestParcelActivity extends Activity implements
OnClickListener { private Button setSub2XML;
private TextView mTextView;
private MyParcel myColor; private Drawable defDrawable; /** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.submain); setSub2XML = (Button) findViewById(R.id.btn02);
setSub2XML.setOnClickListener(this); mTextView = (TextView)findViewById(R.id.sub_tv);
defDrawable = mTextView.getBackground(); Intent intent = getIntent();
if (intent != null) {
if (intent.hasExtra("MyColor")) {
myColor = intent.getParcelableExtra("MyColor");
mTextView.setBackgroundColor(myColor.getColor());
}
}
} @Override
protected void onStart() {
super.onStart();
} @Override
protected void onRestart() {
super.onRestart();
} @Override
protected void onResume() {
super.onResume();
} @Override
protected void onPause() {
super.onPause();
} @Override
protected void onStop() {
super.onStop();
} @Override
protected void onDestroy() {
super.onDestroy();
} @Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn02:
Intent mIntent = new Intent(getApplicationContext(),
Sub2MyTestParcelActivity.class);
if (myColor != null) {
myColor.setColor(Color.GREEN);
mIntent.putExtra("MyColor", myColor);
} mTextView.setBackgroundDrawable(defDrawable);
setResult(RESULT_OK, mIntent);
finish();
break; default:
break;
}
} }

使用binder类型代码

package com.parcel.main;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class Sub2MyTestParcelActivity extends Activity implements
OnClickListener { private Button setMainXML;
private TextView mTextView;
private BinderMyParcel myColor; private Drawable defDrawable; /** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.sub2main); setMainXML = (Button) findViewById(R.id.btn03);
setMainXML.setOnClickListener(this); mTextView = (TextView) findViewById(R.id.sub_tv);
defDrawable = mTextView.getBackground(); Intent intent = getIntent();
if (intent != null) {
if (intent.hasExtra("BinderMyColor")) {
myColor = intent.getParcelableExtra("BinderMyColor");
mTextView.setBackgroundColor(myColor.getColor());
}
}
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn03:
Intent mIntent = new Intent(getApplicationContext(),
MyTestParcelActivity.class);
if (myColor != null) {
myColor.setColor(Color.WHITE);
mIntent.putExtra("BinderMyColor", myColor);
} mTextView.setBackgroundDrawable(defDrawable);
setResult(RESULT_OK, mIntent);
finish();
break; default:
break;
}
} }

非Binder类型的Parcel,实现Parcelable接口即可:

package com.parcel.main;

import android.graphics.Color;
import android.os.Parcel;
import android.os.Parcelable; public class MyParcel implements Parcelable {
private int color = Color.BLACK; public int getColor() {
return color;
} public void setColor(int color) {
this.color = color;
} public MyParcel() {
color = Color.BLACK;
} public MyParcel(Parcel in) {
color = in.readInt();
} @Override
public int describeContents() {
return ;
} @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(color);
} public static final Parcelable.Creator<MyParcel> CREATOR = new Parcelable.Creator<MyParcel>() { public MyParcel createFromParcel(Parcel in) {
return new MyParcel(in);
} public MyParcel[] newArray(int size) {
return new MyParcel[size];
}
}; }

对于Binder类型,首先要实现一个Binder接口(其,最主要的区别在于反序列化仍然是原来的对象,而非Binder类型的反序列化并不是原来的对象,所以在MainActivity中的onActivityResult方法中会出现mMyParcel = data.getParcelableExtra("MyColor")的写法):

package com.parcel.main;

import android.os.Binder;

public class BinderData extends Binder {

    public int color;

}

其次再实现Parcelable接口:

package com.parcel.main;

import android.graphics.Color;
import android.os.Parcel;
import android.os.Parcelable; public class BinderMyParcel implements Parcelable { private BinderData data = new BinderData(); public BinderMyParcel() {
data.color = Color.BLUE;
} public BinderMyParcel(Parcel in) {
data = (BinderData) in.readValue(BinderData.class.getClassLoader());
} public void setColor(int color) {
data.color = color;
} public int getColor() {
return data.color;
} @Override
public int describeContents() {
return ;
} @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(data);
} public static final Parcelable.Creator<BinderMyParcel> CREATOR = new Creator<BinderMyParcel>() { @Override
public BinderMyParcel[] newArray(int size) {
return new BinderMyParcel[size];
} @Override
public BinderMyParcel createFromParcel(Parcel source) {
return new BinderMyParcel(source);
}
};
}

三个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:id="@+id/main_tv" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/main_activity" />
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:text="SetSubMainXML" android:id="@+id/btn01"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/btn04" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="SetSub2MainXML" />
</LinearLayout> </LinearLayout>
<?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/sub_tv" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/sub_activity" />
<Button android:text="setMainXML" android:id="@+id/btn02"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<?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/sub_tv" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/sub2_activity" />
<Button android:text="setMainXML" android:id="@+id/btn03"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>

Android中的Parcel机制 实现Bundle传递对象的更多相关文章

  1. 探索Android中的Parcel机制(上)

    一.先从Serialize说起 我们都知道JAVA中的Serialize机制,译成串行化.序列化……,其作用是能将数据对象存入字节流其中,在须要时又一次生成对象.主要应用是利用外部存储设备保存对象状态 ...

  2. Android中的Parcel机制(上)

    一.先从Serialize说起 我们都知道JAVA中的Serialize机制,译成串行化.序列化--,其作用是能将数据对象存入字节流当中,在需要时重新生成对象.主要应用是利用外部存储设备保存对象状态, ...

  3. Android中的Parcel机制(下)

    上一篇中我们透过源码看到了Parcel背后的机制,本质上把它当成一个Serialize就可以了,只是它是在内存中完成的序列化和反序列化,利用的是连续的内存空间,因此会更加高效. 我们接下来要说的是Pa ...

  4. 在Android中通过Intent使用Bundle传递对象

    IntentBundle传递对象SerializableParcelable Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法.被传递的对象 ...

  5. 浅析Android中的消息机制(转)

    原博客地址:http://blog.csdn.net/liuhe688/article/details/6407225 在分析Android消息机制之前,我们先来看一段代码: public class ...

  6. 浅析Android中的消息机制(转)

    在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...

  7. 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.

    在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...

  8. 浅析Android中的消息机制

    在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...

  9. 重温Android中的消息机制

    引入: 提到Android中的消息机制,大家应该都不陌生,我们在开发中不可避免的要和它打交道.从我们开发的角度来看,Handler是Android消息机制的上层接口.我们在平时的开发中只需要和Hand ...

随机推荐

  1. oledb方式读取excel文件

    进入博客园后台发现12年11月份写的草稿没发,时隔1年,把它拉出来晒晒太阳. 前言 第一次做Excel文件导入,采用了oledb,不足之处,还请各位大牛指出,谨以此文对导入Excel做个总结. 一般步 ...

  2. My97DatePicker 没有权限问题

    引自:http://blog.sina.com.cn/s/blog_4b7809800100wkv4.html 今天遇到了My97DatePicker在不同IE版本中使用时有时出现没有权限错误的问题, ...

  3. mysql嵌套查询

    select * from(select t.`name` `name`,count(*) count from company t group by t.`name`) aa where aa.co ...

  4. 在 ServiceModel 客户端配置部分中,找不到引用协定“PmWs.PmWebServiceSoap”的默认终结点元素

    System.Exception: ConfigManager.LoadConfigurationFromDb ServiceFactory.GetPmWebService 在 ServiceMode ...

  5. weewwe

    http://blog.csdn.net/u013073524/article/details/25912891

  6. HDU 4706 Children's Day(简单模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4706 题目大意:要用‘a’-‘z’ 26个小写字母输出倒着写得字母'N'的形状,例如 a ebdfc ...

  7. 【CODECHEF】【phollard rho + miller_rabin】The First Cube

    All submissions for this problem are available. Read problems statements in Mandarin Chinese and Rus ...

  8. java编程思想,对象导论

    程序设计的本质就是使用编程语言解决某一类具体问题.对问题的定义叫建模,例如定义问题域中的各种名词,动作,结果等.针对具体的问题提出的解决方案叫算法. 面向对象程序设计的挑战之一,就是在问题空间的元素和 ...

  9. cli下的php(并传递参数)

    传递参数有两种方式: 第一种使用文件操作,STDOUT作为标准输出,STDIN作为标准输入 使用fwrite($file,$string)作输出,使用fgets($file)作输入.这种应该算是继承自 ...

  10. python 数据运算

    算数运算: