coco2dx与java之间的互调的问题
java调用c++的函数
1.声明java函数
public class ReqJavaToC{
public native static void SetAudioState(boolean bState);
}
2.jni/hellocpp下增加一个ReqJavaToC.h, ReqJavaToC.cpp
ReqJavaToC.h,
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JavaTC */
#ifndef _Included_ReqJavaToC
#define _Included_ReqJavaToC
#endif
ReqJavaToC:
#include "ReqJavaToC.h"
#include "../../Classes/JniMethod.h"
extern "C"
{
int g_product_id = 0;
void Java_com_echashu_game_jpgame_ReqJavaToC_SetAudioState(JNIEnv *, jclass,jboolean bState){
//OnSucceed();
JniMethod::SetAudioState(bState);
}
}
2.把cpp添加到android.mk中
c++调用java的函数
1.通过发消息
public class ShowPayHelper {
private static Handler mHandler;
public static void init(Handler handler) {
ShowPayHelper.mHandler = handler;
}
public static void PayMon(int i) {
Message msg = mHandler.obtainMessage();
msg.what = JPGame.PAY_TYPE_ONE;
msg.sendToTarget();
}
}
2. 在activity中添加消息接收
Handler aHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case PAY_TYPE_ONE:
//order(context, PAYCODE_ONE, mListener);
break;
}}}
3.
#if (CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
JniMethodInfo minfo;
bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/echashu/ShowPayHelper ","PayMon","(I)V");
if (!isHave){
CCLog("jni:�˺������");
}
else
{
CCLog("jni:�˺������");
minfo.env->CallStaticVoidMethod(minfo.classID,minfo.methodID,823);
}
CCLog("jni:����ִ�����");
#endif