将 int 从本机代码(jni)传递给 java,在 java 中修改它并通过参数传回
我的本机代码(c++)中有一个线程,该线程使用参数中传递的整数调用java中的回调函数。 java中的回调函数应该修改这个整数并返回true或false。 本机代码可以使用新的(修改后的)整数。
示例代码:
C 代码:
doSomething()
{
int id = 5;
callback_fct(&id)
}
callback_fct(int* id)
{
jclass integerClass = (env)->FindClass("java/lang/Integer");
jmethodID midConstructor = (env)->GetMethodID(integerClass, "<init>", "(I)V");
jmethodID midIntValue = (env)->GetMethodID(integerClass, "intValue", "()I");
jobject integerObject = (env)->NewObject(integerClass, midConstructor, *id);
//id should be 5
int res = env->CallIntMethod(appl_object, mid, integerObject);
*id= env->CallIntMethod(integerObject, midIntValue);
//id should be 99 now
}
Java 代码:
private int callback( Integer ID)
{
Log.i("JavaWrapper", "callback");
Log.i("JavaWrapper", "Old ID: " + ID); //should be 5
ID = 99;
Log.i("JavaWrapper", "New ID: " + ID); //should be 99
return 0;
}
有人能给我一个简短的例子吗?
谢谢
I have a thread in my native Code (c++) and this thread calls a callback function in java with a passed integer in agument.
The callback function in java should modify this integer and returns true or false.
The native code sould work with the new (modified) integer.
Example Code:
C-Code:
doSomething()
{
int id = 5;
callback_fct(&id)
}
callback_fct(int* id)
{
jclass integerClass = (env)->FindClass("java/lang/Integer");
jmethodID midConstructor = (env)->GetMethodID(integerClass, "<init>", "(I)V");
jmethodID midIntValue = (env)->GetMethodID(integerClass, "intValue", "()I");
jobject integerObject = (env)->NewObject(integerClass, midConstructor, *id);
//id should be 5
int res = env->CallIntMethod(appl_object, mid, integerObject);
*id= env->CallIntMethod(integerObject, midIntValue);
//id should be 99 now
}
Java-Code:
private int callback( Integer ID)
{
Log.i("JavaWrapper", "callback");
Log.i("JavaWrapper", "Old ID: " + ID); //should be 5
ID = 99;
Log.i("JavaWrapper", "New ID: " + ID); //should be 99
return 0;
}
Can someone give me a short example for this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论