Android Bundle 实例介绍
文章目录
####1、Bundle简介
Bundle 主要用于传递数据,它保存的数据,是以Key-Value (键值对)的形式存在的。
1)我们经常使用的Bundle在Activity之间传递数据,传递的数据可以是:boolean 、byte、int、long、float、double、string等基本类型,或者他们对应的数组。
2)也可以是对象或者对象数组,当bundle传递的是对象或者对象数组的时候,对象必须实现Serializable或者Parcelable接口。
3)下面介绍activity 之间如何传递基本类型、传递对象的。
####2、传递基本类型
Bundle提供了各种常用的类型的putXxx()/getXxx()方法,用于读写基本类型的数据。Bundle操作基本数据类型的API接口:
bundle 写 数据的方法如下:
// "com.test" is the package name of the destination class (第一个参数是包名) // "com.test.Activity02" is the full class path of the destination class (第二个参数是具体到那一个activity)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 bundle = this.getIntent().getExtras(); String name = bundle.getString("name"); int height = bundle.getInt("height");
####3、传递Parcelable类型的对象
#####1)Parcelable说明
parcelable 是Android自定义的一个接口,它包括了将数据写入Parcel和从Parcel中读出API.一个实体(用类来表示),如果需要封装到bundle消息中,可以通过实现Parcelable接口来实现。
Parcelable 和 Serializable的API如下表:
#####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)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类型的对象
#####1)Serializable说明
(1)Serializable是一个对象序列化的接口。一个类只有实现了Serializable接口,它的对象才是可序列化的。
(2)因此如果要序列化某些类的对象,这些类就必须实现Serializable接口。
(3)实际上,Serializable 是一个空接口,没有什么具体的内容,它的目的只是简单的标志一个类的对象可以被序列化。
#####2)Serializable 接口的实现方法
很简单,只要implements Serializable 接口就可以了
####5、Demo 实例
通过对上述三种情况下的数据 通过Bundle 传输,来更深刻了解Bundle.
#####1)demo 概要
BundleTest 一共包含了4个java文件 和 2 个layout 文件
Bundle01.java —— 默认的主Activity窗口。
Bundle02.java —— 主Activity用于跳转的目的窗口。
Book.java —— 实现Parcelable接口的类
Person.java —— 实现Serializable接口的类
main.xml —— Bundle01.java的layout文件
main2.xml —— Bundle02.java的layout文件
工程文件结构如下所示:
#####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>
######string.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; } }
#####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如下:
文章参考:
Android Bundle详解
https://blog.csdn.net/cswhale/article/details/39053411#t1
Android Bundle 实例介绍相关推荐
- Android Fragment 基本介绍
Android Fragment 基本介绍 Android Fragment 基本介绍 Fragment Android是在Android 3.0 (API level 11)开始引入Fragment ...
- Android Bundle类 学习总结
Android Bundle类 http://blog.csdn.net/randyjiawenjie/article/details/6651437 根据google官方的文档(http://dev ...
- Android AIDL使用介绍(2)自定义数据类型的传递
1.背景 默认情况下,AIDL只支持下列数据类型: Java八种基础数据类型(如 int.long.char.boolean 等); String字符串: CharSequence字符序列: List ...
- 【分享】Android JNI实例
[分享]Android JNI实例 Android的SDK中没有包括JNI的支持,而且对如何支持JNI也没有任何文档说明.不过既然整个Android平台是开源的,我们可以通过Google发布的源代码 ...
- Android常用实例——截取APP当前界面(可带图片、文字水印)
Android常用实例--截取APP当前界面(可带图片.文字水印) 标签: android界面截图保存图片 2016-08-16 10:52 1262人阅读 评论(2) 收藏 举报 分类: Andr ...
- android bundle 对象,Android Bundle传递对象
首先Android的Bundle是可以传递对象的.我们可以用Bundle b = new Bundle():b.putSerializable("key", 对象引用); 但是这样 ...
- 【转】 Android常用实例—Alert Dialog的使用
Android常用实例-Alert Dialog的使用 AlertDialog的使用很普遍,在应用中当你想要用户做出"是"或"否"或者其它各式各样的选择时,为了 ...
- Android中SlidingDrawer介绍【安卓进化三十四】
Android中SlidingDrawer介绍[安卓进化三十四] 安卓中1.5后加入了SlidingDrawer[隐藏式抽屉],设计原理在你的UI布局有限的情况下,放不下太多的控件的时候,可以考虑用这 ...
- Android开发实例-Android平台手机新闻客户端
Android开发实例<Android平台手机新闻客户端>是基于Android4.0及以上平台的一款新闻类手机应用,应用运行效果如下所示: Android开发实例课程主要介绍运行于Andr ...
最新文章
- iOS指示器之UIProgressView和UIPageControl
- Leetcode中Path的题目总结
- 暑训day1解题报告
- 【Java】CMD编译Java源码遇到\ufeff问题的解决方法
- 【英语学习】【Level 07】U05 Best Destination L4 A perfect destination
- 简明java_简明 Java 错误处理机制
- docker容器与大数据组件的冲突点
- 用示波器对单片机I2C时序进行图形波形分析的试验小结
- tiledmap 图块属性_TiledMap详解
- 备考新手指南--QA手册
- 10分钟搭建一个H5商城,支持微信支付和各平台小程序
- python爬取起点中文网_Python3爬取起点中文网阅读量信息,解决文字反爬~~~附源代码...
- 外文论文查重怎么查?
- 什么是视频结构化?视频结构化有什么作用
- 基于Python PIL库的简易马赛克拼图程序
- 关于Spring项目配置多个数据源的实例演示
- mac 如何安装/运行 kakfa
- 图像模式识别 (五)
- Android 虚拟按键上报
- Java实现满天星动案例
热门文章
- linux搭建socks5代理
- 【iOS开发】Masonry的使用
- Cover Letter指南
- JavaScript(运算符、逻辑结构、循环)——笔记整理0512
- BUUCTF-reverse-reverse3(超级详细)
- dataGridView选中行的数据
- “大师兄”社区(http://www.daxixiong.com/)欢迎你!!!
- mysql插入数据到多个表,MySQL插入多个表?(数据库规范化?)
- 网络打印机识别不了工作组计算机无法访问,无法访问工作组计算机和不能共享打印机解决办法...
- ASP.NET 路由实现去除aspx后缀