cocos2d-x之C++ 调用Java函数并接收返回值

cocos2d-x之C++ 调用Java函数并接收返回值,第1张

概述参考:http://blog.csdn.net/xiaominghimi/article/details/7602984 </pre><pre code_snippet_id="1607324" snippet_file_name="blog_20160312_3_5324342" name="code" class="cpp">//java中string转C++中string <pre name
参考:http://blog.csdn.net/xiaominghimi/article/details/7602984
</pre><pre code_snippet_ID="1607324" snippet_file_name="blog_20160312_3_5324342" name="code" >//java中string转C++中string
<pre name="code" >#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)//Jstring to char*std::string JstringTostring(jnienv* env,Jstring Jstr){	char* rtn = NulL;	jclass clsstring = env->FindClass("java/lang/String");	Jstring strencode = env->NewStringUTF("utf-8");	jmethodID mID = env->getmethodID(clsstring,"getBytes","(Ljava/lang/String;)[B");	jbyteArray barr= (jbyteArray)env->CallObjectMethod(Jstr,mID,strencode);	Jsize alen = env->GetArrayLength(barr);	jbyte* ba = env->GetByteArrayElements(barr,JNI_FALSE);	if (alen > 0)	{		rtn = (char*)malloc(alen + 1);		memcpy(rtn,ba,alen);		rtn[alen] = 0;	}	env->ReleaseByteArrayElements(barr,0);	std::string stemp(rtn);	free(rtn);	return stemp;}#endif

//调用手机信息接口std::string callJinPhoneNum() {	std::string strNum;	//AndroID:调用手机号码接口#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)	JniMethodInfo minfo;	bool isHave;	isHave = JniHelper::getStaticmethodInfo(minfo,PACKAGE_name,"getPhoneNum","()Ljava/lang/String;");	if (!isHave) {		log("JNI:exitGame method is not exist");	}	else{		Jstring JsNum = (Jstring)minfo.env->CallStaticObjectMethod(minfo.classID,minfo.methodID);		strNum = JstringTostring(minfo.env,JsNum);	}#endif	return strNum;}
voID callJinTime(std::string strTime) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; bool isHave; isHave = JniHelper::getStaticmethodInfo(minfo,"getTime","(Ljava/lang/String;)V"); if (!isHave) { log("JNI:exitGame method is not exist"); } else{ Jstring Jstr = minfo.env->NewStringUTF(strTime.c_str()); minfo.env->CallStaticObjectMethod(minfo.classID,minfo.methodID,Jstr); } #endif } 总结

以上是内存溢出为你收集整理的cocos2d-x之C++ 调用Java函数接收返回值全部内容,希望文章能够帮你解决cocos2d-x之C++ 调用Java函数并接收返回值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078271.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存