double getDoubleForKeyJNI(const char* pKey, double defaultValue)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getDoubleForKey", "(Ljava/lang/String;D)D")) {
jstring stringArg = t.env->NewStringUTF(pKey);
jdouble ret = t.env->CallStaticDoubleMethod(t.classID, t.methodID, stringArg);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
return ret;
}
return defaultValue;
}
上面的代码摘自Java_org_cocos2dx_lib_Cocos2dxHelper.cpp(cocos2dx源码),
这里只关注字符串的传递,字符串通过下面的函数,把C++的字符转化为jstring:
jstring stringArg = t.env->NewStringUTF(pKey);
然后传递给下面的函数:
CallStaticDoubleMethod
使用完后,一定要记得释放:
<pre name="code" class="cpp"> t.env->DeleteLocalRef(t.classID);
<span style="color:#ff0000;"> t.env->DeleteLocalRef(stringArg);</span>