Android中Parcel的分析以及使用
簡單點來說:Parcel就是一個存放讀取數據的容器, Android系統中的binder進程間通信(IPC)就使用了Parcel類來進行客戶端與服務端數據的交互,而且AIDL的數據也是通過Parcel來交互的。在Java空間和C++都實現了Parcel,由於它在C/C++中,直接使用了內存來讀取數據,因此,它更有效率。
分析Binder機制中的客戶端與服務器端進行實際操作ontransact()函數 :
[java]- //參數說明:
- // code :是請求的ID號
- // data :客戶端請求發送的參數
- // reply:服務器端返回的結果
- // flags:一些額外的標識,如FLAG_ONEWAY等,通常為0.
- virtual status_t onTransact( uint32_t code,
- const Parcel& data,
- Parcel* reply,
- uint32_t flags = 0);
從中我們可以看到Parcel的重要性以及窺探它的使用情況,接下來,我主要分析它的存儲機制。
常用方法介紹:
obtain() 獲得一個新的parcel ,相當於new一個對象
dataSize() 得到當前parcel對象的實際存儲空間
dataCapacity() 得到當前parcel對象的已分配的存儲空間, >=dataSize()值 (以空間換時間)
dataPostion() 獲得當前parcel對象的偏移量(類似於文件流指針的偏移量)
setDataPosition() 設置偏移量
recyle() 清空、回收parcel對象的內存
writeInt(int) 寫入一個整數
writeFloat(float) 寫入一個浮點數
writeDouble(double) 寫入一個雙精度數
writeString(string) 寫入一個字符串
當然,還有更多的writeXXX()方法,與之對應的就是readXXX(),具體方法請參閱SDK。
其中幾個值得註意的方法為:
writeException() 在Parcel隊頭寫入一個異常
writeException() Parcel隊頭寫入“無異常“
readException() 在Parcel隊頭讀取,若讀取值為異常,則拋出該異常;否則,程序正常運行。
一、Parcel的分析
相信看了前面的值,對Parcel的使用該有了初步印象。那麽,Parcel的內部存儲機制是怎麽樣的?偏移量又是
什麽情況?讓我們回憶一下基本數據類型的取值範圍:
boolean 1bit 1字節
char 16bit 2字節
int 32bit 4字節
long 64bit 8字節
float 32bit 4字節
double 64bit 8字節
如果大家對C語言熟悉的話,C語言中結構體的內存對齊和Parcel采用的內存存放機制一樣,即讀取最小字節
為32bit,也即4個字節。高於4個字節的,以實際數據類型進行存放,但得為4byte的倍數。基本公式如下:
實際存放字節:
判別一: 32bit (<=32bit) 例如:boolean,char,int
判別二: 實際占用字節(>32bit) 例如:long,float,String,數組等
當我們使用readXXX()方法時,讀取方法也如上述:
實際讀取字節:
判別一: 32bit (<=32bit) 例如:boolean,char,int
判別二: 實際字節大小(>32bit) 例如:long,float,String,數值等
由上可以知道,當我們寫入/讀取一個數據時,偏移量至少為4byte(32bit),於是,偏移量的公式如下:
f(x)= 4x (x=0,1,…n)
事實上,我們可以顯示的通過setDataPostion(int postion) 來直接操作我們欲讀取數據時的偏移量。毫無疑問,
你可以設置任何偏移量,但所讀取的值是類型可能有誤。因此顯示設置偏移量讀取值的時候,需要小心。
另外一個註意點就是我們在writeXXX()和readXXX()時,導致的偏移量是共用的,例如,我們在writeInt(23)後,
此時的datapostion=4,如果我們想讀取5,簡單的通過readInt()是不行的,只能得到0。這時我們只能通過
setDataPosition(0)設置為起始偏移量,從起始位置讀取四個字節,即23。因此,在讀取某個值時,可能需要使用
setDataPostion(int postion)使偏移量裝換到我們的值處。
巧用setDataPosition()方法,當我們的parcel對象中只存在某一類型時,我們就可以通過這個方法來快速的讀取
所有值。具體方法如下:
[html]
- /**
- * 前提條件,Parcel存在多個類型相同的對象,本例子以10個float對象說明:
- */
- public void readSameType() {
- Parcel parcel =Parcel.obtain() ;
- for (int i = 0; i < 10; i++) {
- parcel.writeDouble(i);
- Log.i(TAG, "write double ----> " + getParcelInfo());
- }
- //方法一 ,顯示設置偏移量
- int i = 0;
- int datasize = parcel.dataSize();
- while (i < datasize) {
- parcel.setDataPosition(i);
- double fvalue = parcel.readDouble();
- Log.i(TAG, " read double is=" + fvalue + ", --->" + getParcelInfo());
- i += 8; // double占用字節為 8byte
- }
- // 方法二,由於對象的類型一致,我們可以直接利用readXXX()讀取值會產生偏移量
- // parcel.setDataPosition(0) ; //
- // while(parcel.dataPosition()<parcel.dataSize()){
- // double fvalue = parcel.readDouble();
- // Log.i(TAG, " read double is=" + fvalue + ", --->" + getParcelInfo());
- // }
- }
由於可能存在讀取值的偏差,一個默認的取值規範為:
1、 讀取復雜對象時: 對象匹配時,返回當前偏移位置的該對象;
對象不匹配時,返回null對象 ;
2、 讀取簡單對象時: 對象匹配時,返回當前偏移位置的該對象 ;
對象不匹配時,返回0;
下面,給出一張淺顯的Parcel的存放空間圖,希望大家在理解的同時,更能體味其中滋味。有點簡單,求諒解。
相信通過前面的介紹,你一定很了解了了Parcel的存儲機制,下面給定一應用程序來實踐。
1、布局文件如下:
[html]- <?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" />
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent" android:layout_height="wrap_content">
- <Button android:id="@+id/btWriteByte" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="寫入一個byte值"></Button>
- <Button android:id="@+id/btWriteInt" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="寫入一個int值"></Button>
- </LinearLayout>
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent" android:layout_height="wrap_content">
- <Button android:id="@+id/btWriteDouble" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="寫入一個double值"></Button>
- <Button android:id="@+id/btWriteString" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="寫入一個String值"></Button>
- </LinearLayout>
- <View android:layout_width="fill_parent" android:layout_height="2dip"
- android:background="#FF1493"></View>
- <LinearLayout android:orientation="horizontal"
- android:layout_marginTop="5dip" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button android:id="@+id/btReadByte" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="讀取一個byte值"></Button>
- <Button android:id="@+id/btReadInt" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="讀取一個int值"></Button>
- </LinearLayout>
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent" android:layout_height="wrap_content">
- <Button android:id="@+id/btReadDouble" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="讀取一個double值"></Button>
- <Button android:id="@+id/btReadString" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="讀取一個String值"></Button>
- </LinearLayout>
- <View android:layout_width="fill_parent" android:layout_height="2dip"
- android:background="#FF1493"></View>
- <Button android:id="@+id/btSameType" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="利用setDataPosition讀取多個值"></Button>
- </LinearLayout>
2、配置文件如下:
[html]
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.qinjuning.parcel"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
3、程序主文件如下:
[html]- public class MainActivity extends Activity implements OnClickListener {
- private static String TAG = "PARCELTEST";
- // Button ID
- private static int[] btIds = new int[] { R.id.btWriteByte, R.id.btWriteInt,
- R.id.btReadDouble, R.id.btWriteString, R.id.btReadByte,
- R.id.btReadInt, R.id.btReadDouble, R.id.btReadString,
- R.id.btSameType };
- // 每種類型的當前值
- private byte cur_byte = 1; // 每次總寫入 false
- private int cur_int = 10; // 寫入值 cur_int ++ ;
- private double cur_float = 100.0d; // 寫入值 cur_float++ ;
- private String cur_str = "QinJun -->" + cur_int; // 寫入值 "QinJun -->"+cur_int
- private Parcel parcel = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- for (int i = 0; i < btIds.length; i++) {
- Button bt = (Button) findViewById(btIds[i]);
- bt.setOnClickListener(this);
- }
- parcel = Parcel.obtain(); // 獲得一個Parcel對象 ,相當於new一個,初始大小為0
- Log.i(TAG, "The original parcel info" + getParcelInfo());
- }
- @Override
- public void onClick(View view) {
- // TODO Auto-generated method stub
- int viewviewId = view.getId();
- switch (viewId) {
- case R.id.btWriteByte:
- parcel.setDataPosition(0);
- parcel.writeByte(cur_byte);
- Log.i(TAG, " after write byte, --->" + getParcelInfo());
- break;
- case R.id.btWriteInt:
- parcel.writeInt(cur_int);
- Log.i(TAG, " after write int, --->" + getParcelInfo());
- break;
- case R.id.btWriteDouble:
- parcel.writeDouble(cur_float);
- Log.i(TAG, " after write float, --->" + getParcelInfo());
- break;
- case R.id.btWriteString:
- parcel.writeString(cur_str);
- Log.i(TAG, " after write String, --->" + getParcelInfo());
- break;
- case R.id.btReadByte:
- byte b = parcel.readByte();
- Log.i(TAG, " read byte is=" + b + ", --->" + getParcelInfo()
- + "String");
- break;
- case R.id.btReadInt:
- int i = parcel.readInt();
- Log.i(TAG, " read int is=" + i + ", --->" + getParcelInfo());
- break;
- case R.id.btReadDouble:
- float f = parcel.readFloat();
- readSameType();
- Log.i(TAG, " read float is=" + f + ", --->" + getParcelInfo());
- break;
- case R.id.btReadString:
- parcel.setDataPosition(0);
- String str = parcel.readString();
- Log.i(TAG, " read float is=" + str + ", --->" + getParcelInfo());
- break;
- case R.id.btSameType:
- readSameType();
- break;
- default:
- break;
- }
- }
- private String getParcelInfo() {// 得到parcel的信息
- return "dataSize = " + parcel.dataSize() + ", dataCapacity="
- + parcel.dataCapacity() + ", dataPositon = "
- + parcel.dataPosition();
- }
- /**
- * 前提條件,Parcel存在多個類型相同的對象,本例子以10個float對象說明:
- */
- public void readSameType() {
- for (int i = 0; i < 10; i++) {
- parcel.writeDouble(i);
- Log.i(TAG, "write double ----> " + getParcelInfo());
- }
- //方法一 ,顯示設置偏移量
- int i = 0;
- int datasize = parcel.dataSize();
- while (i < datasize) {
- parcel.setDataPosition(i);
- double fvalue = parcel.readDouble();
- Log.i(TAG, " read double is=" + fvalue + ", --->" + getParcelInfo());
- i += 8; // double占用字節為 8byte
- }
- // 方法二,由於對象的類型一致,我們可以直接利用readXXX()讀取值會產生偏移量
- // parcel.setDataPosition(0) ; //
- // while(parcel.dataPosition()<parcel.dataSize()){
- // double fvalue = parcel.readDouble();
- // Log.i(TAG, " read double is=" + fvalue + ", --->" + getParcelInfo());
- // }
- }
- }
由於取值時,可能存在類型的轉換,因此點擊按鈕時,可能不會產生預期結果。因此,得保證偏移量對應數值的正確性。
Android中Parcel的分析以及使用