標籤:android des style blog class code
Parcelable使用的一個地方就是在進程間傳遞一個自訂的較為複雜的對象,對Parcelable學習了一下寫下自己的觀點。
將一個對象比如說一個名字為Cartoon的自訂類,由一個activity傳遞到另外一個activity去,這個時候就要用到Parcelable了。首先我們先構建一個bean。
package com.example.sendobjfromactivity.bean;import android.graphics.Bitmap;public class Cartoon { private Bitmap figure; private String name; private String creator; public Bitmap getFigure() { return figure; } public void setFigure(Bitmap figure) { this.figure = figure; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCreator() { return creator; } public void setCreator(String creator) { this.creator = creator; }}
這就是我們要傳輸的資料的格式,下面就思考,要將其線上程間進行傳輸,就要對其實現序列化,在Android中也就是實現Parcelable介面,這個借口中需要我們實現的主要方法有writeToParcel,describe,ContentsParcelable.Creator<?>。完成完成序列化是依靠writeToParcel方法,而對對象進行還原序列化並且返回一個對象執行個體的是依靠ContentsParcelable.Creator<?>。
下面來看這樣一段代碼,ParcelableCartoon類實現了Parcelable介面
package com.example.sendobjfromactivity.bean;import android.graphics.Bitmap;import android.os.Parcel;import android.os.Parcelable;import android.util.Log;public class ParcelableCartoon implements Parcelable { private Cartoon cartoon; private static final String MSG = "MESSAGE"; public ParcelableCartoon(Cartoon cartoon){ Log.i(MSG, "ParcelableCartoon::[email protected]"); this.cartoon = cartoon; } // 將對象寫入Parcel容器中去 // 完成對對象的序列化 /** * Flatten this object in to a Parcel. * * @param dest The Parcel in which the object should be written. * @param flags Additional flags about how the object should be written. * May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}. */ @Override public void writeToParcel(Parcel dest, int flags) { Log.i(MSG, "ParcelableCartoon::writeToParcel"); dest.writeString(cartoon.getName()); dest.writeString(cartoon.getCreator()); dest.writeParcelable(cartoon.getFigure(), PARCELABLE_WRITE_RETURN_VALUE); } // 完成對序列化的對象還原序列化 public static final Parcelable.Creator<ParcelableCartoon> CREATOR = new Parcelable.Creator<ParcelableCartoon>(){ // 從Parcel容器中擷取序列化的對象,並將其還原序列化,得到該對象的執行個體 /** * Create a new instance of the Parcelable class, instantiating it * from the given Parcel whose data had previously been written by * {@link Parcelable#writeToParcel Parcelable.writeToParcel()}. * * @param source The Parcel to read the object‘s data from. * @return Returns a new instance of the Parcelable class. */ @Override public ParcelableCartoon createFromParcel(Parcel source) { Log.i(MSG, "ParcelableCartoon::Parcelable.Creator::createFromParcel"); return new ParcelableCartoon(source); } @Override public ParcelableCartoon[] newArray(int size) { Log.i(MSG, "ParcelableCartoon::Parcelable.Creator::newArray"); return new ParcelableCartoon[size]; } }; public ParcelableCartoon(Parcel in){ Log.i(MSG, "ParcelableCartoon::[email protected]"); cartoon = new Cartoon(); String name = in.readString(); cartoon.setName(name); cartoon.setCreator(in.readString()); cartoon.setFigure((Bitmap)in.readParcelable(Bitmap.class.getClassLoader())); } public Cartoon getCartoon() { Log.i(MSG, "ParcelableCartoon::getCartoon"); return cartoon; } @Override public int describeContents() { Log.i(MSG, "ParcelableCartoon::describeContents"); return 0; }}
在該類中用英文的注釋引用了Android文檔中的注釋。在該類中涉及到一個Parcel,它是一個容器,它的主要任務是對需要序列化的對象進行寫入,對需要還原序列化的對象進行寫出(構造一個執行個體)。
完成了這些,就可以對對象進行序列化了,也就是說我們可以傳遞對象了。
下面完成倆個activity,一個負責發送資料,一個用來接收資料。
SendObjFromActivity.java完成發送
package com.example.sendobjfromactivity;import java.io.InputStream;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.example.sendobjfromactivity.bean.Cartoon;import com.example.sendobjfromactivity.bean.ParcelableCartoon;public class SendObjFromActivity extends Activity { private Button sendData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sendData = (Button)this.findViewById(R.id.button1); sendData.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Cartoon cartoon = new Cartoon(); cartoon.setName("Android"); cartoon.setCreator("Google"); InputStream is = getResources().openRawResource(R.drawable.ic_launcher); Bitmap bitmap = BitmapFactory.decodeStream(is); cartoon.setFigure(bitmap); Intent intent = new Intent(SendObjFromActivity.this, ReceiveObjActivity.class); // 完成對象的序列化操作 ParcelableCartoon parcelableCartoon = new ParcelableCartoon(cartoon); intent.putExtra("msg", parcelableCartoon); startActivity(intent); }}); }}
ReceiveObjActivity.java完成對資料的接收
package com.example.sendobjfromactivity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.widget.ImageView;import android.widget.TextView;import com.example.sendobjfromactivity.bean.Cartoon;import com.example.sendobjfromactivity.bean.ParcelableCartoon;public class ReceiveObjActivity extends Activity { private ImageView showImage; private TextView showText; @Override protected void onCreate(Bundle savedInstanceState) { Log.i("MESSAGE", "進入到了另外的一個activity中去"); super.onCreate(savedInstanceState); setContentView(R.layout.display_obj); showImage = (ImageView)this.findViewById(R.id.imageView1); showText = (TextView)this.findViewById(R.id.textView1); Intent intent = getIntent(); ParcelableCartoon parcelableCartoon = (ParcelableCartoon) intent .getParcelableExtra("msg"); Cartoon cartoon = parcelableCartoon.getCartoon(); showImage.setImageBitmap(cartoon.getFigure()); showText.setText(cartoon.getName()+"\n"+cartoon.getCreator()); }}
在這個程式中會調用Parcelable.Creator<ParcelableCartoon>內的方法,來完成還原序列化。
需要到兩個xml布局檔案:display_obj.xml,main.xml。比較簡單不再敘述。
在AndroidManifest.xml中配置下activity。這樣就完成了一個小的資料轉送程式了,看下運行結果
在控制台中輸出來的資訊
這樣可以看出來Parcelable的運行方式以及順序。