zoukankan      html  css  js  c++  java
  • JNI调用java方法

    调用java静态方法

    jclass led = env->GetObjectClass(jclassled);
    
    //  获取id
    jmethodID getLedId = env->GetStaticMethodID(led, "getLedId", "()I");
    LOGE("#######getLedId 
    ");
    if (getLedId == NULL)
    {
        LOGE("#######error getLedId
    ");
        return -1; /* method not found */
    }
    jint id = env->CallIntMethod(led, getLedId);
     LOGE("#######CallIntMethod 
    ");
    //  获取color
    jmethodID getColor = env->GetStaticMethodID(led, "getColor", "()[I");
    if (getColor == NULL) 
    {
        LOGE("#######error getColor
    ");
        return -1; /* method not found */
    }
    jint color[3] = {0}; 
    jobjectArray resultArray = (jobjectArray) env->CallObjectMethod(led, getColor);
    for (int i = 0; i < env->GetArrayLength(resultArray); i++) 
    {
        color[i] = (jint) env->GetObjectArrayElement(resultArray, i);
    }
    
    LOGE("#######id = %d, r=%d, g=%d, b=%d
    ", id, color[0], color[1], color[2]);
    //  设置led
    set_led(id, color[0], color[1], color[2]);
    

    调用java实例方法

    jclass led = env->FindClass("com/deptech/common/base/utils/commonUtils/Led");
    if (led == NULL)
    {
        LOGE("#######not found class.
    ");
        return -1;
    }
    
    // 构造方法
    jmethodID mid_construct = env->GetMethodID(led, "<init>","()V");
    if (mid_construct == NULL) 
    {
        LOGE("#######not found construct func.
    ");
        return -1;
    }
    
    // 获取getLedId方法
    jmethodID getLedId = env->GetMethodID(led, "getLedId", "()I");
    if (getLedId == NULL)
    {    
        LOGE("#######not found getLedId func.
    ");
        return -1;
    }
    
    jmethodID getColorR = env->GetMethodID(led, "getColorR", "()I");
    if (getColorR == NULL)
    {
        LOGE("#######not found getColorR func.
    ");
        return -1;
    }
    jmethodID getColorG = env->GetMethodID(led, "getColorG", "()I");
    if (getColorG == NULL)
    {
        LOGE("#######not found getColorG func.
    ");
        return -1;
    }
    jmethodID getColorB = env->GetMethodID(led, "getColorB", "()I");
    if (getColorB == NULL)
    {
        LOGE("#######not found getColorB func.
    ");
        return -1;
    }
    
    //  使用构造方法实例化
    jobject ledObj = env->NewObject(led, mid_construct);
    if (ledObj == NULL) {
        LOGE("#######error NewObject.
    ");
        return -1;
    }
    
    // 调用实例方法
    // 获取id
    jint id = env->CallIntMethod(ledObj, getLedId);
    LOGE("#######CallIntMethod %d.
    ", id);
    
    //  获取color
    jint r, g, b;
    r = env->CallIntMethod(ledObj, getColorR);
    g = env->CallIntMethod(ledObj, getColorG);
    b = env->CallIntMethod(ledObj, getColorB);
    
    
    jobjectArray resultArray = (jobjectArray) env->CallObjectMethod(ledObj, getColor);
    if (resultArray == NULL)
    {
        LOGE("#######error resultArray is null.
    ");
        return -1;
    }
    for (int i = 0; i < env->GetArrayLength(resultArray); i++) 
    {
        color[i] = (long)env->GetObjectArrayElement(resultArray, i);
    }
    
    LOGE("#######CallObjectMethod.
    ");
    
    //  设置led
    LOGE("#######id = %d, r=%d, g=%d, b=%d
    ", id, r, g, b);
    set_led(id, r, g, b);
    
    // 释放
    env->DeleteLocalRef(led);
    env->DeleteLocalRef(ledObj);
    //env->DeleteLocalRef(resultArray);
  • 相关阅读:

    【工作】---前后端联调
    【react】---Immutable的基本使用
    【react】传值
    【原生】 HTML DOM 事件,各种事件类型、事件种类
    两台笔记本电脑之间实现屏幕扩展
    【看图学习后台管理系统】
    【bug】在react开发中,使用link 跳转中,无法点击跳转的问题
    【前端工程师】 web 安全问题
    【前端工程师】 性能和效率 优化的问题
  • 原文地址:https://www.cnblogs.com/xiongyungang/p/12469337.html
Copyright © 2011-2022 走看看