Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android framwork cpp調用java function

Android framwork cpp調用java function

編輯:關於Android編程

這裡以android_view_PointerIcon.cpp的函數getSystemIcon為例。
1、在gPointerIconClassInfo的結構體中,定義了jmethodID getSystemIcon,並且在

register_android_view_PointerIcon中進行初始化設置;

static struct {
    jclass clazz;
    jfieldID mStyle;
    jfieldID mBitmap;
    jfieldID mHotSpotX;
    jfieldID mHotSpotY;
    jmethodID getSystemIcon;
    jmethodID load;
} gPointerIconClassInfo;



int register_android_view_PointerIcon(JNIEnv* env) {
    FIND_CLASS(gPointerIconClassInfo.clazz, "android/view/PointerIcon");

    GET_FIELD_ID(gPointerIconClassInfo.mBitmap, gPointerIconClassInfo.clazz,
            "mBitmap", "Landroid/graphics/Bitmap;");

    GET_FIELD_ID(gPointerIconClassInfo.mStyle, gPointerIconClassInfo.clazz,
            "mStyle", "I");

    GET_FIELD_ID(gPointerIconClassInfo.mHotSpotX, gPointerIconClassInfo.clazz,
            "mHotSpotX", "F");

    GET_FIELD_ID(gPointerIconClassInfo.mHotSpotY, gPointerIconClassInfo.clazz,
            "mHotSpotY", "F");

    GET_STATIC_METHOD_ID(gPointerIconClassInfo.getSystemIcon, gPointerIconClassInfo.clazz,
            "getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;");

    GET_METHOD_ID(gPointerIconClassInfo.load, gPointerIconClassInfo.clazz,
            "load", "(Landroid/content/Context;)Landroid/view/PointerIcon;");

    return 0;
}

GET_STATIC_METHOD_ID(gPointerIconClassInfo.getSystemIcon, gPointerIconClassInfo.clazz,
"getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;");
第一個傳入參數會被初始化為java層的getSystemIcon引用,第三個參數即是java層函數名,第四個是java層函數的輸入輸出參數描述

2、register_android_view_PointerIcon是在AndroidRuntime.cpp中運行的;

extern int register_android_view_PointerIcon(JNIEnv* env);

static const RegJNIRec gRegJNI[] = {

    REG_JNI(register_android_view_MotionEvent),
    REG_JNI(register_android_view_PointerIcon),
    REG_JNI(register_android_view_VelocityTracker),

};

3、使用:

    jobject android_view_PointerIcon_getSystemIcon(JNIEnv* env, jobject contextObj, int32_t style) {
    jobject pointerIconObj = env->CallStaticObjectMethod(gPointerIconClassInfo.clazz,
            gPointerIconClassInfo.getSystemIcon, contextObj, style);
    if (env->ExceptionCheck()) {
        ALOGW("An exception occurred while getting a pointer icon with style %d.", style);
        LOGW_EX(env);
        env->ExceptionClear();
        return NULL;
    }
    return pointerIconObj;
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved