• 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);
  • 相关阅读:
    *Server对象的URLEncode方法的详细介绍 *
    *Server对象的方法简单介绍*
    *SQLDB中图片的读取并显示*
    *关于DataBinder.Eval*
    SQLMaps 的基本思想
    *DropdownList的数据绑定*
    面向对象的一些思想
    [zz]NoSQL对比:Cassandra vs MongoDB vs CouchDB vs Redis vs Riak vs HBase vs Membase vs Neo4j
    [zz]HDFS文件读写 使用c api
    [zz]linux patch 简单使用速查
  • 原文地址:https://www.cnblogs.com/xiongyungang/p/12469337.html
Copyright © 2020-2023  润新知