我试图使用Android中的JNI从我的C代码调用Java函数,但我处于一种有点尴尬的境地。
我的C代码正在传递给库的回调中的JNI函数之外执行。
以下是java代码的示例
package com.my.java.package;
class MyClass {
public function handleData(byte[] data) {
doSomethingWithThisData(data);
}
}
以下是C代码的示例
void handleData(uint8_t *data, size_t len) {
// I need to call handleData in my java
// class instance from here, but i have
// no access to a JNIEnv here.
// I don't think I can create one, since
// it has to be the same object that's
// sending JNI calls elsewhere.
}
. . .
myCLibInstance.callback = handleData;
现在每当C Lib执行它需要做的事情时,它都会触发回调。但我无法将其发送回java类来处理数据。
答案 0 :(得分:2)
在某些版本的Android NDK上,JNI_GetCreatedJavaVMs
可用于获取当前的VM ..但是,更好的选择是覆盖JNI_OnLoad
并将VM保存在那里。使用任一方法,一旦拥有VM,就可以附加到当前线程并调用函数..
extern jint JNI_GetCreatedJavaVMs(JavaVM **vm, jsize size, jsize *size2);
static JavaVM *jvm = NULL;
static jint JNI_OnLoad(JavaVM* vm, void* reserved) {
jvm = vm;
JNIEnv *env = NULL;
if (jvm && (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6) == JNI_OK)
{
return JNI_VERSION_1_6;
}
return -1;
}
JavaVM* getJavaVM() {
if (jvm)
{
return jvm;
}
jint num_vms = 0;
const jint max_vms = 5;
JavaVM* vms[max_vms] = {0};
if (JNI_GetCreatedJavaVMs(vms, max_vms, &num_vms) == JNI_OK)
{
for (int i = 0; i < num_vms; ++i)
{
if (vms[i] != NULL)
{
return vms[i];
}
}
}
return NULL;
}
void handleData(uint8_t *data, size_t len) {
JavaVM *jvm = getJavaVM();
if (jvm)
{
JNIEnv *env = NULL;
if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL) == JNI_OK)
{
if (env)
{
//Call function with JNI..
}
(*jvm)->DetachCurrentThread(jvm);
}
}
}
答案 1 :(得分:0)
我注意到Brandons Solution的一些问题,特别是关于您处理状态码和不必要的getJavaVM
函数的方式,因此我进行了一些更改并添加了一些注意事项。这是我设法开始使用的唯一功能版本。
请注意,由于某些原因,
JNIEnv*
返回的getJNIEnv()
与Java类加载器 在另一个线程中使用 时不兼容。我不确定为什么。因此,在此示例中,我将静态实例存储到类中,直接将它们加载到JNI_OnLoad
函数中,并在以后需要时使用它们。如果有人知道要解决的办法是让
JNIEnv*
返回getJNIEnv()
来支持其他线程的Java类加载器,请告诉我。
// JavaVM instance stored after JNI_OnLoad is called
JavaVM* javaVM = NULL;
// Since the class loader will not work with getJNIEnv(),
// you can store classes in GlobalRefs.
static jclass my_class_class;
/**
* Load the JNIEnv and store the JavaVM instance for ater calls to getJNIEnv().
*
* @param jvm The Java VM
* @param reserved Reserved pointer
*
* @return The supported version of JNI.
*/
JNIEXPORT jint JNICALL
JNI_OnLoad(
JavaVM* jvm,
void* reserved
) {
javaVM = jvm;
// Here we load the classes since getJNIEnv() does
// not work with the class loader from other threads
JNIEnv* env = getJNIEnv();
my_class_class = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/my/java/package/MyClass"));
// Return the supported JNI version.
return JNI_VERSION_1_6;
}
/**
* Retrieve an instance of JNIEnv to use across threads.
*
* Note that the class loader will not work with this instance (unsure why).
*
* @return a JNIEnv instance
*/
JNIEnv* getJNIEnv() {
JNIEnv *env;
// If the current thread is not attached to the VM,
// sets *env to NULL, and returns JNI_EDETACHED.
//
// If the specified version is not supported, sets *env to NULL,
// and returns JNI_EVERSION.
//
// Otherwise, sets *env to the appropriate interface, and returns JNI_OK.
int status = (*javaVM)->GetEnv(javaVM, (void**)&env, JNI_VERSION_1_6);
// Check if the JVM is not currently attached to the
// calling thread, and if so attempt to attach it.
if (status == JNI_EDETACHED) {
// Attaches the current thread to a Java VM.
// Returns a JNI interface pointer in the JNIEnv argument.
status = (*javaVM)->AttachCurrentThread(javaVM, &env, NULL);
}
// If the result of GetEnv was JNI_EVERSION,
// we want to abort.
assert(status != JNI_EVERSION);
// Return the ENV if we have one
return env;
}
void handleData(uint8_t *data, size_t len) {
JNIEnv* env = getJNIEnv();
// ... call jni function using env and my_class_class ...
}