http://blog.csdn.net/cswhale/article/details/39053411html
1 Bundle介紹
Bundle主要用於傳遞數據;它保存的數據,是以key-value(鍵值對)的形式存在的。java
咱們常常使用Bundle在Activity之間傳遞數據,傳遞的數據能夠是boolean、byte、int、long、float、double、string等基本類型或它們對應的數組,也能夠是對象或對象數組。當Bundle傳遞的是對象或對象數組時,必須實現Serializable 或Parcelable接口。下面分別介紹Activity之間如何傳遞基本類型、傳遞對象。android
2傳遞基本類型
Bundle提供了各類經常使用類型的putXxx()/getXxx()方法,用於讀寫基本類型的數據。Bundle操做基本數據類型的API表格以下所示:數組
寫數據的方法以下:app
- 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);
-
- finish();
對應的讀數據的方法以下:ide
- 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);
-
-
- 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是一個空接口,沒有什麼具體內容,它的目的只是簡單的標識一個類的對象能夠被序列化。this
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
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:
- {
-
-
- Intent intent = new Intent().setClassName("com.bundletest", "com.bundletest.Bundle01");
- startActivity(intent);
-
- 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中Bundle類的做用 Bundle類用做攜帶數據,它相似於Map,用於存放key-value名值對形式的值 |
今天發現本身連Bundle類都沒有搞清楚,因而花時間研究了一下。
根據google官方的文檔(http://developer.android.com/reference/android/os/Bundle.html)
Bundle類是一個key-value對,「A mapping from String values to various Parcelable types.」
類繼承關係:
Java.lang.Object
Android.os.Bundle
Bundle類是一個final類:
public final class
Bundle
extends Objectimplements Parcelable Cloneable
兩個activity之間的通信能夠經過bundle類來實現,作法就是:
(1)新建一個bundle類
- Bundle mBundle = new Bundle();
(2)bundle類中加入數據(key -value的形式,另外一個activity裏面取數據的時候,就要用到key,找出對應的value)
- mBundle.putString("Data", "data from TestBundle");
(3)新建一個intent對象,並將該bundle加入這個intent對象
- Intent intent = new Intent();
- intent.setClass(TestBundle.this, Target.class);
- intent.putExtras(mBundle);
完整代碼以下:
android mainfest.xml以下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.tencent.test"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".TestBundle"
- 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=".Target"></activity>
- </application>
- <uses-sdk android:minSdkVersion="7" />
- </manifest>
兩個類以下:intent從TestBundle類發起,到Target類。
類1:TestBundle類:
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class TestBundle extends Activity {
-
- private Button button1;
- private OnClickListener cl;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- button1 = (Button) findViewById(R.id.button1);
- cl = new OnClickListener(){
- @Override
- public void onClick(View arg0) {
-
- Intent intent = new Intent();
- intent.setClass(TestBundle.this, Target.class);
- Bundle mBundle = new Bundle();
- mBundle.putString("Data", "data from TestBundle");
- intent.putExtras(mBundle);
- startActivity(intent);
- }
- };
- button1.setOnClickListener(cl);
- }
- }
類2: Target
- import android.app.Activity;
- import android.os.Bundle;
-
- public class Target extends Activity{
-
- public void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.target);
- <span style="color:#ff6600;">Bundle bundle = getIntent().getExtras(); </span>
- String data = bundle.getString("Data");
- setTitle(data);
-
- }
- }
佈局文件:
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"
- />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/button"
- android:id = "@+id/button1"
- />
- </LinearLayout>
target.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/target"
- />
- </LinearLayout>
String.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, TestBundle!</string>
- <string name="app_name">測試Bundle用法</string>
- <string name="button">點擊跳轉</string>
- <string name="target">來到target activity</string>
- </resources>
結果:
跳轉結果:
http://blog.csdn.net/luman1991/article/details/52887533