Android中JNI進階應用程式 – 本地C代碼中建立Java對象及本地JNI對象的儲存 .

來源:互聯網
上載者:User

本地C代碼中建立Java對象

建立Java域的對象就是建立Java類的執行個體,再調用Java類的構造方法。

以Bitmap的構建為例,Bitmap中並沒有Java對象建立的代碼及外部能訪問的構造方法,所以它的執行個體化必然是在JNI的c中實現的。

BitmapFactory.java中提供了得到Bitmap的方法,時序簡化為:

BitmapFactory.java ->BitmapFactory.cpp -> GraphicsJNI::createBitmap()  [graphics.cpp]

GraphicsJNI::createBitmap()[graphics.cpp]的實現:

jobjectGraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, bool isMutable,                                  jbyteArray ninepatch, intdensity){    SkASSERT(bitmap != NULL);    SkASSERT(NULL != bitmap->pixelRef());    jobject obj =env->AllocObject(gBitmap_class);    if (obj) {        env->CallVoidMethod(obj,gBitmap_constructorMethodID,                            (jint)bitmap,isMutable, ninepatch, density);        if (hasException(env)) {            obj = NULL;        }    }    return obj;}

 

而gBitmap_class的得到是通過:

jclass c =env->FindClass("android/graphics/Bitmap");gBitmap_class =(jclass)env->NewGlobalRef(c);//gBitmap_constructorMethodID是Bitmap的構造方法(方法名用”<init>”)的jmethodID:gBitmap_constructorMethodID= env->GetMethodID(gBitmap_class, "<init>",                                           "(IZ[BI)V");

 

總結一下,c中如何訪問Java對象的屬性:

1)        通過JNIEnv::FindClass()找到對應的jclass;

2)        通過JNIEnv::GetMethodID()找到類的構造方法的jfieldID;

3)        通過JNIEnv::AllocObject建立該類的對象;

4)        通過JNIEnv::CallVoidMethod()調用Java對象的構造方法。

本地JNI對象的儲存

c域中某次被調用產生的對象,在其他函數調用時是不可見的,雖然可以設定全域變數但那不是好的解決方式,Android中通常是在Java域中定義一個int型的變數,在c域產生對象的地方,與這個Java域的變數關聯,在別的使用到的地方,在從這個變數中取值。

 

以JNICameraContext為例來說明:

JNICameraContext是android_hardware_camera.cpp中定義的類型,並會在cpp中產生對象,與Java中android.hardware.Camera的mNativeContext關聯。

在註冊native函數之前,c中就已經把Java域中的屬性的jfieldID得到了。通過下列方法

jclass clazz = env->FindClass("android/hardware/Camera ");jfieldID field = env->GetFieldID(clazz, " mNativeContext","I");

如果執行成功,把field儲存到上面圖中的fileds變數的context:jfieldID中。

 

產生cpp對象時,通過JNIEnv::SetIntField()設定為Java對象的屬性

static void android_hardware_Camera_native_setup(JNIEnv *env, jobjectthiz,    jobject weak_this, jintcameraId){    // …    // We use a weak reference sothe Camera object can be garbage collected.    // The reference is only used asa proxy for callbacks.    sp<JNICameraContext>context = new JNICameraContext(env, weak_this, clazz, camera);    // …    // 該處通過context.get()得到context對象的地址,儲存到了Java中的mNativeContext屬性裡    env->SetIntField(thiz,fields.context, (int)context.get());}

而要使用時,又通過JNIEnv::GetIntField()擷取Java對象的屬性,並轉化為JNICameraContext類型:

    JNICameraContext* context =reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));    if (context != NULL) {        // …    }

總結一下,c++中產生的對象如何儲存和使用:

1)   通過JNIEnv::FindClass()找到對應的jclass;

2)   通過JNIEnv::GetFieldID()找到類中屬性的jfieldID;

3)   某個調用過程中,產生cpp對象時,通過JNIEnv::SetIntField()設定為Java對象的屬性;

4)   另外的調用過程中,通過JNIEnv::GetIntField()擷取Java對象的屬性,再轉化為真實的物件類型。

------------------------------------------------原帖地址-------------------------------------------------------------
感謝mr_raptor:

http://blog.csdn.net/mr_raptor/article/details/7401178

--------------------------------------------------------------------------------------------------------------------------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.