下面就线程外回调java进行介绍:
1找到类句柄
2找到类的方法的句柄
3实例化类
4调用实例化类的方法。
以上是基本步骤。
JavaVM *gJavaVM;
jmethodID mid;
jclass mClass;‘
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv *env;
gJavaVM = vm;
int result;
//LOGI("JNI_OnLoad called");
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
//LOGE("Failed to get the environment using GetEnv()");
return -1;
}
java_class = env->FindClass("com/tymx/zndx/ServiceTransaction");
if (java_class == 0)
{
__android_log_print(ANDROID_LOG_DEBUG, "jni", "not find class!!");
return -1;//env->NewStringUTF("not find class!");
}
mClass = (jclass)env->NewGlobalRef(java_class);
result = JNI_VERSION_1_4;
return result;
}
这是找到class
char * HttpUpFile(char * fileName)
{
int status;
JNIEnv *env;
bool isAttached = false;
status = gJavaVM->GetEnv((void **) &env, JNI_VERSION_1_4);
if(status < 0) {
status = gJavaVM->AttachCurrentThread(&env, NULL);
if(status < 0) {
return NULL;
}
isAttached = true;
}
jstring str = env -> NewStringUTF(fileName);
mid = env->GetStaticMethodID(mClass, "HttpUpFile","(Ljava/lang/String;)Ljava/lang/String;");
//-----------------------------------------------------------
//env->CallStaticVoidMethod(mClass, mid, str);
jstring barr = (jstring)env->CallStaticObjectMethod(mClass, mid, str);
__android_log_print(ANDROID_LOG_DEBUG, "jni", "old Name : %s",fileName);
char * rtn = NULL;
rtn = jstringTostring(env,barr);
//-----------------------------------------------------------
__android_log_print(ANDROID_LOG_DEBUG, "jni", "new Name : %s",rtn);
if(isAttached)
gJavaVM->DetachCurrentThread();
return rtn;
}
这就是开始调用java中的HttpUpFile函数,传入一个参数,返回一个参数,HttpUpFile这个函数是静态的,不知道为什么,只有是静态才成功!!!
参考资料:
步骤:http://hi.baidu.com/lihn1987/blog/item/8eefe3d9ca97883b32fa1cfa.html
参数签名:http://public0821.iteye.com/blog/423941
回调:http://www.android1.net/Topic.aspx?BoardID=11&TopicID=1199