Android——Android Bundle详解(转)
Android Bundle详解
1 Bundle介绍
Bundle主要用于传递数据;它保存的数据,是以key-value(键值对)的形式存在的。
我们经常使用Bundle在Activity之间传递数据,传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。当Bundle传递的是对象或对象数组时,必须实现Serializable 或Parcelable接口。下面分别介绍Activity之间如何传递基本类型、传递对象。
2传递基本类型
Bundle提供了各种常用类型的putXxx()/getXxx()方法,用于读写基本类型的数据。Bundle操作基本数据类型的API表格如下所示:

写数据的方法如下:
// "com.test" is the package name of the destination class
// "com.test.Activity02" is the full class path of the destination class
Intent intent = new Intent().setClassName("com.bundletest", "com.bundletest.Bundle02"); Bundle bundle = new Bundle();
bundle.putString("name", "skywang");
bundle.putInt("height", 175);
intent.putExtras(bundle); startActivity(intent); // end current class
finish();
对应的读数据的方法如下:
Bundle bundle = this.getIntent().getExtras();
String name = bundle.getString("name");
int height = bundle.getInt("height");
3传递Parcelable类型的对象
3.1 Parcelable说明
Parcelable是Android自定义的一个接口,它包括了将数据写入Parcel和从Parcel中读出的API。一个实体(用类来表示),如果需要封装到bundle消息中去,可以通过实现Parcelable接口来实现。
Parcelable和Serializable的API如下表:

3.2 Parcelable接口说明
public interface Parcelable {
//内容描述接口,基本不用管
public int describeContents();
//写入接口函数,打包
public void writeToParcel(Parcel dest, int flags);
//读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入。
//为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例。
public interface Creator<T> {
public T createFromParcel(Parcel source);
public T[] newArray(int size);
}
}
3.3 Parcelable接口的实现方法
从parcelable接口定义中,我们可以看到,实现parcelable接口,需要我们实现下面几个方法:
(01)describeContents方法。内容接口描述,默认返回0就可以;
(02)writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中.即打包需要传递的数据到Parcel容器保存,以便从parcel容器获取数据,该方法声明如下:
writeToParcel(Parcel dest, int flags) 具体参数含义见doc文档
(3.)静态的Parcelable.Creator接口,本接口有两个方法:
createFromParcel(Parcelin) 从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层。
newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话(returnnew T[size])即可。方法是供外部类反序列化本类数组使用。
4传递Serializable类型的对象
4.1 Serializable说明
Serializable是一个对象序列化的接口。一个类只有实现了Serializable接口,它的对象才是可序列化的。因此如果要序列化某些类的对象,这些类就必须实现Serializable接口。而实际上,Serializable是一个空接口,没有什么具体内容,它的目的只是简单的标识一个类的对象可以被序列化。
4.2 Serializable接口的实现方法
很简单,只要implements Serializable接口就可以了
5 demo演示程序
下面是对实现上述三种数据传递方式的BundleTest(demo程序)进行简要介绍
5.1 demo概要
BundleTest共包含了4个java文件和2个layout文件(main.xml和main2.xml)
Bundle01.java —— 默认的主Activity窗口。
Bundle02.java —— 主Activity用于跳转的目的窗口。
Book.java —— 实现Parcelable接口的类
Person.java —— 实现Serializable接口的类
main.xml —— Bundle01.java的layout文件
main2.xml —— Bundle02.java的layout文件
工程文件结构如下所示:

5.2代码
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bundletest"
android:versionCode="1"
android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Bundle01"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Bundle02"> </activity>
</application>
<uses-sdk android:minSdkVersion="11" />
</manifest>
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/app_01"
/> <Button
android:id="@+id/btnBasic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/text_basic"
/> <Button
android:id="@+id/btnPar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/text_par"
/> <Button
android:id="@+id/btnSer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/text_ser"
/> </LinearLayout>
main2.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/app_02"
/> <Button
android:id="@+id/btnBack"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/text_jump_back"
/> </LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello MyBundleTest!</string>
<string name="app_name">MyBundleTest</string>
<string name="app_01">Bundle_01</string>
<string name="app_02">Bundle_02</string>
<string name="text_basic">Bundle Basic Data</string>
<string name="text_par">Bundle Parcelable Data</string>
<string name="text_ser">Bundle Seriable Data</string>
<string name="text_jump_back">Jump Back to Bundler01</string>
</resources>
Bundle01.java
package com.bundletest; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
import android.util.Log; public class Bundle01 extends Activity implements View.OnClickListener{ private static final String TAG = "skywang-->Bundle01"; private Button mBtnBasic = null;
private Button mBtnPar = null;
private Button mBtnSer = null; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); mBtnBasic = (Button) findViewById(R.id.btnBasic);
mBtnBasic.setOnClickListener(this); mBtnPar = (Button) findViewById(R.id.btnPar);
mBtnPar.setOnClickListener(this); mBtnSer = (Button) findViewById(R.id.btnSer);
mBtnSer.setOnClickListener(this);
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnBasic:
sendBasicDataThroughBundle();
break;
case R.id.btnPar:
sendParcelableDataThroughBundle();
break;
case R.id.btnSer:
sendSeriableDataThroughBundle();
break;
default:
break; }
} // sent basic data, such as int, strin, etc... through bundle
private void sendBasicDataThroughBundle(){
// "com.test" is the package name of the destination class
// "com.test.Activity02" is the full class path of the destination class
Intent intent = new Intent().setClassName("com.bundletest", "com.bundletest.Bundle02"); Bundle bundle = new Bundle();
bundle.putString("name", "skywang");
bundle.putInt("height", 175);
intent.putExtras(bundle); startActivity(intent); // end current class
finish();
} // sent object through Pacelable
private void sendParcelableDataThroughBundle(){
Intent intent = new Intent().setClassName("com.bundletest", "com.bundletest.Bundle02"); Book mBook = new Book();
mBook.setBookName("Android");
mBook.setAuthor("skywang");
mBook.setPublishTime(2013); Bundle mBundle = new Bundle();
mBundle.putParcelable("ParcelableValue", mBook);
intent.putExtras(mBundle); startActivity(intent);
finish();
} // sent object through seriable
private void sendSeriableDataThroughBundle(){
Intent intent = new Intent().setClassName("com.bundletest", "com.bundletest.Bundle02"); Person mPerson = new Person();
mPerson.setName("skywang");
mPerson.setAge(24); Bundle mBundle = new Bundle();
mBundle.putSerializable("SeriableValue",mPerson);
intent.putExtras(mBundle); startActivity(intent);
finish();
} }
Bundle02.java
package com.bundletest; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
import android.util.Log; public class Bundle02 extends Activity implements View.OnClickListener { private static final String TAG = "skywang-->Bundle02"; private Button mBtnBack = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2); mBtnBack = (Button) findViewById(R.id.btnBack);
mBtnBack.setOnClickListener(this); receiveBasicData();
receiveParcelableData();
receiveSeriableData();
} private void receiveBasicData() {
Bundle bundle = this.getIntent().getExtras(); String name = bundle.getString("name");
int height = bundle.getInt("height");
if (name != null && height != 0)
Log.d(TAG, "receice basic data -- " +
"name="+name+", height="+height);
} private void receiveParcelableData() {
Book mBook = (Book)getIntent().getParcelableExtra("ParcelableValue");
if (mBook != null)
Log.d(TAG, "receice parcel data -- " +
"Book name is: " + mBook.getBookName()+", "+
"Author is: " + mBook.getAuthor() + ", "+
"PublishTime is: " + mBook.getPublishTime());
} private void receiveSeriableData() {
Person mPerson = (Person)getIntent().getSerializableExtra("SeriableValue");
if (mPerson != null)
Log.d(TAG, "receice serial data -- " +
"The name is:" + mPerson.getName() + ", "+
"age is:" + mPerson.getAge());
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnBack:
{
// "com.test" is the package name of the destination class
// "com.test.Activity01" is the full class path of the destination class
Intent intent = new Intent().setClassName("com.bundletest", "com.bundletest.Bundle01");
startActivity(intent);
// end current class
finish();
}
break;
default:
break; }
} }
Book.java
package com.bundletest; import android.os.Parcel;
import android.os.Parcelable; public class Book implements Parcelable {
private String bookName;
private String author;
private int publishTime; public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPublishTime() {
return publishTime;
}
public void setPublishTime(int publishTime) {
this.publishTime = publishTime;
} public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
@Override
public Book createFromParcel(Parcel source) {
Book mBook = new Book();
mBook.bookName = source.readString();
mBook.author = source.readString();
mBook.publishTime = source.readInt();
return mBook;
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
}; @Override
public int describeContents() {
return 0;
} @Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(bookName);
parcel.writeString(author);
parcel.writeInt(publishTime);
}
}
Person.java
package com.bundletest;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
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;
}
}
5.3输出图片
Bundle01.java对应的界面如下:

点击“Bundle Basic Data”、“Bundle Parcelable Data”、“Bundle Seriable Data”均跳转到如下界面,但它们对应的logcat信息不同。

点击“Bundle Basic Data”的logcat如下:

点击“Bundle Parcelable Data”的logcat如下:

点击“Bundle Seriable Data”的logcat如下:

转自:http://www.cnblogs.com/skywang12345/archive/2013/03/06/3165555.html
Android——Android Bundle详解(转)的更多相关文章
- Android之canvas详解
首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, y ...
- 【转】Android Canvas绘图详解(图文)
转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html Android Canvas绘图详解(图文) 泡 ...
- Android GLSurfaceView用法详解(二)
输入如何处理 若是开发一个交互型的应用(如游戏),通常需要子类化 GLSurfaceView,由此可以获取输入事件.下面有个例子: java代码: package eoe.ClearTes ...
- Android Fragment用法详解(2)--动态添加Fragment
在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...
- Android lifecycle 使用详解
版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/gdutxiaoxu/article/det ...
- Android 自定义 View 详解
View 的绘制系列文章: Android View 绘制流程之 DecorView 与 ViewRootImpl Android View 的绘制流程之 Measure 过程详解 (一) Andro ...
- Android AIDL使用详解_Android IPC 机制详解
一.概述 AIDL 意思即 Android Interface Definition Language,翻译过来就是Android接口定义语言,是用于定义服务器和客户端通信接口的一种描述语言,可以拿来 ...
- Android apk签名详解——AS签名、获取签名信息、系统签名、命令行签名
Apk签名,每一个Android开发者都不陌生.它就是对我们的apk加了一个校验参数,防止apk被掉包.一开始做Android开发,就接触到了apk签名:后来在微信开放平台.高德地图等平台注册时,需要 ...
- android:ToolBar详解
android:ToolBar详解(手把手教程) 泡在网上的日子 发表于 2014-11-18 12:49 第 124857 次阅读 ToolBar 42 来源 http://blog.mosil.b ...
随机推荐
- 语音信号处理之(一)动态时间规整(DTW)
语音信号处理之(一)动态时间规整(DTW) zouxy09@qq.com 原文:http://blog.csdn.net/zouxy09 这学期有<语音信号处理>这门课,快考试了,所以也要 ...
- Ext3.0中复杂表头样例
注意要点:不出现滚动栏时要设置height和forceFit : false 效果例如以下图: this.columns = [{ header : '月份', dataIndex : '月份', w ...
- Discuz常见小问题-如何删除用户
用户-用户管理,直接拉到底部点搜索 切换到最后一页,然后勾选要删除的用户(最后一页就是最新注册的用户) 要勾选两个选项,删除过程比较长,不要中途退出,等待删除完毕 再次搜索用户就少一个了
- 谷歌Gmail 加速
由于某些原因的限制,我们使用谷歌的Gmail服务时,网络加载总是很慢!如下修复 一:修改hosts文件 ping -c g.cn 得到ip地址 在hosts文件里面 添加 上面的 ip地址 ...
- 40、JDBC相关概念介绍
1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡,同样道理,我们安装好数据 ...
- Linux命令-文件处理命令:ln
ln -s /etc/issue /tmp/issue.soft 为这个/etc/issue文件创建一个软连接,名称为/tmp/issue.soft(相当于windows里面给/etc/issue文件 ...
- Android+手势识别详解
今天就来把以前的学习文章与经验简单总结中出来吧,在这里我就直接把代码贴下来了,希望能给初学者做最佳的学习参考,也希望有更多的开发人员来加入 ANDROID开发团队,参与更多的创新方式的开发,好了, ...
- 容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例
一.容器适配器 stack queue priority_queue stack.queue.priority_queue 都不支持任一种迭代器,它们都是容器适配器类型,stack是用vector/d ...
- C++多态有哪几种方式?
C++多态方式: (1)静态多态(重载,模板) 是在编译的时候,就确定调用函数的类型. (2)动态多态(覆盖,虚函数实现) 在运行的时候,才确定调用的是哪个函数,动态绑定.运行基类指针指向派生类的对象 ...
- Redis 实现接口访问频率限制
为什么限制访问频率 做服务接口时通常需要用到请求频率限制 Rate limiting,例如限制一个用户1分钟内最多可以范围100次 主要用来保证服务性能和保护数据安全 因为如果不进行限制,服务调用者可 ...