jni call java_JNI调用java方法

调用java静态方法

jclass led = env->GetObjectClass(jclassled);

// 获取id

jmethodID getLedId = env->GetStaticMethodID(led, "getLedId", "()I");

LOGE("#######getLedId \n");

if (getLedId == NULL)

{

LOGE("#######error getLedId\n");

return -1; /* method not found */

}

jint id = env->CallIntMethod(led, getLedId);

LOGE("#######CallIntMethod \n");

// 获取color

jmethodID getColor = env->GetStaticMethodID(led, "getColor", "()[I");

if (getColor == NULL)

{

LOGE("#######error getColor\n");

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\n", 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.\n");

return -1;

}

// 构造方法

jmethodID mid_construct = env->GetMethodID(led, "","()V");

if (mid_construct == NULL)

{

LOGE("#######not found construct func.\n");

return -1;

}

// 获取getLedId方法

jmethodID getLedId = env->GetMethodID(led, "getLedId", "()I");

if (getLedId == NULL)

{

LOGE("#######not found getLedId func.\n");

return -1;

}

jmethodID getColorR = env->GetMethodID(led, "getColorR", "()I");

if (getColorR == NULL)

{

LOGE("#######not found getColorR func.\n");

return -1;

}

jmethodID getColorG = env->GetMethodID(led, "getColorG", "()I");

if (getColorG == NULL)

{

LOGE("#######not found getColorG func.\n");

return -1;

}

jmethodID getColorB = env->GetMethodID(led, "getColorB", "()I");

if (getColorB == NULL)

{

LOGE("#######not found getColorB func.\n");

return -1;

}

// 使用构造方法实例化

jobject ledObj = env->NewObject(led, mid_construct);

if (ledObj == NULL) {

LOGE("#######error NewObject.\n");

return -1;

}

// 调用实例方法

// 获取id

jint id = env->CallIntMethod(ledObj, getLedId);

LOGE("#######CallIntMethod %d.\n", 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.\n");

return -1;

}

for (int i = 0; i < env->GetArrayLength(resultArray); i++)

{

color[i] = (long)env->GetObjectArrayElement(resultArray, i);

}

LOGE("#######CallObjectMethod.\n");

// 设置led

LOGE("#######id = %d, r=%d, g=%d, b=%d\n", id, r, g, b);

set_led(id, r, g, b);

// 释放

env->DeleteLocalRef(led);

env->DeleteLocalRef(ledObj);

//env->DeleteLocalRef(resultArray);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值