/**
* Write an integer value into the parcel at the current dataPosition(),
* growing dataCapacity() if needed.
*/
public final native void writeInt(int val);
/**
* Write a long integer value into the parcel at the current dataPosition(),
* growing dataCapacity() if needed.
*/
public final native void writeLong(long val);
/**
* Write a floating point value into the parcel at the current
* dataPosition(), growing dataCapacity() if needed.
*/
public final native void writeFloat(float val);
static void android_os_Parcel_writeInt(JNIEnv* env, jobject clazz, jint val)
{
Parcel* parcel = parcelForJavaObject(env, clazz);
if (parcel != NULL) {
const status_t err = parcel->writeInt32(val);
if (err != NO_ERROR) {
jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
}
}
}
static void android_os_Parcel_writeLong(JNIEnv* env, jobject clazz, jlong val)
{
Parcel* parcel = parcelForJavaObject(env, clazz);
if (parcel != NULL) {
const status_t err = parcel->writeInt64(val);
if (err != NO_ERROR) {
jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
}
}
}
static void android_os_Parcel_writeFloat(JNIEnv* env, jobject clazz, jfloat val)
{
Parcel* parcel = parcelForJavaObject(env, clazz);
if (parcel != NULL) {
const status_t err = parcel->writeFloat(val);
if (err != NO_ERROR) {
jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
}
}
}
status_t Parcel::writeInt32(int32_t val)
{
return writeAligned(val);
}
--> 直接利用模块实现
template<class T>
status_t Parcel::writeAligned(T val) {
COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
if ((mDataPos+sizeof(val)) <= mDataCapacity) {
restart_write:
*reinterpret_cast<T*>(mData+mDataPos) = val; // 直接在此将数据写入到内存中
return finishWrite(sizeof(val));
}
status_t err = growData(sizeof(val)); // 数据空间不够的情况下处理
if (err == NO_ERROR) goto restart_write;
return err;
}
status_t Parcel::growData(size_t len)
{
size_t newSize = ((mDataSize+len)*3)/2; // 每次多分配50%的内存空间
return (newSize <= mDataSize)
? (status_t) NO_MEMORY
: continueWrite(newSize);
}
举个例子说明一下如何在同一个Activity传递复杂数据类型:
package com.test.testparcel;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import android.view.KeyEvent;
public class TestParcelActivity extends Activity {
ComplexDataStruct complexData = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_0){
testComplexDS();
}
return super.onKeyUp(keyCode, event);
}
public void testComplexDS(){
complexData = new ComplexDataStruct();
Intent intent=new Intent(TestParcelActivity.this,TestParcelActivity.class);
complexData.a = 10;
complexData.b = 20;
complexData.str1 = " hello...";
complexData.str2 = " world...";
Log.i("","intent.putExtra before");
intent.putExtra("testcomplexData", complexData);
Log.i("","intent.putExtra after");
Log.i("","intent.getParcelableExtra before");
ComplexDataStruct test = (ComplexDataStruct)intent.getParcelableExtra("testcomplexData");
Log.i("","intent.getParcelableExtra after");
Log.i("","a="+test.a);
Log.i("","b="+test.b);
Log.i("","str1="+test.str1);
Log.i("","str2="+test.str2);
}
/* 一个复杂的Parcelable对象传递 */
public class ComplexDataStruct implements Parcelable{
public int a = 0;
public int b = 0;
public String str1 = null;
public String str2 = null;
ComplexDataStruct(){
//TODO
}
public ComplexDataStruct(ComplexDataStruct other) {
this.a = other.a;
this.b = other.b;
this.str1 = other.str1;
this.str2 = other.str2;
}
private ComplexDataStruct(Parcel in) {
Log.e("","readFromParcel is calling...");
readFromParcel(in);
}
public void readFromParcel(Parcel in) {
a = in.readInt();
b = in.readInt();
str1 = in.readString();
str2 = in.readString();
Log.e("","readFromParcel is calling...");
}
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel arg0, int arg1) {
// TODO Auto-generated method stub
Log.e("","writeToParcel is calling...");
arg0.writeInt(a);
arg0.writeInt(b);
arg0.writeString(str1);
arg0.writeString(str2);
Log.e("","writeToParcel is calling...");
}
/* 内部实现静态CREATOR类 */
public final Parcelable.Creator<ComplexDataStruct> CREATOR =
new Parcelable.Creator<ComplexDataStruct>() {
// 从Parcel中读取数据,返回ComplexDataStruct对象
public ComplexDataStruct createFromParcel(Parcel in) {
Log.e("","createFromParcel is calling...");
return new ComplexDataStruct(in);
}
public ComplexDataStruct[] newArray(int size) {
Log.e("","newArray is calling...");
return new ComplexDataStruct[size]; // 传递数组
}
};
}
}
4、传递使用 Intent.java 类的函数:putExtra 及 getParcelableXXX函数