0.引进头文件
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include<jni.h>
#include"platform/android/jni/JniHelper.h" //引不到JniHelper.h
#include "JniHelper.h" //编译不能通过
#include<android/log.h>
#endif
1.传递两参数 string类型并且返回一个 string类型
包名org/play/test/、类名CaddCall
void HelloWorld::menuCloseCallback(CCObject* pSender) { bool isHave; const char* msgRet; JniMethodInfo minfo; const char* fileName = "testWei"; //产品id const char* imageName = "testWei"; //包名org/play/test/、类名CaddCall isHave = JniHelper::getStaticMethodInfo(minfo, "org/play/test/CaddCall", "googlePlay", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); if (isHave) { jstring jTitle = minfo.env->NewStringUTF(fileName); jstring jTitle1 = minfo.env->NewStringUTF(imageName); jobject obj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID, jTitle, jTitle1); jstring jRet = (jstring) obj; msgRet = (minfo.env)->GetStringUTFChars(jRet, NULL); } if (std::string(msgRet) == "1") CCMessageBox("1", "1"); else CCMessageBox("0", "0"); }
启动类:继承extends Cocos2dxActivity的类
(1)通过单例的static方法访问,启动类的非static方法
类CaddCall
CaddCall.acty:CaddCall类public static APlay acty; 在xxx类onCreat...时候 CaddCall.acty = this;这个时候在static方法中就能调用xxx类的非static方法了
public static String googlePlay(final String protuctId, String imagePath) { CaddCall.acty.onBuyGasButtonClicked(); return CaddCall.retResult; }
注:通过这种方法也解决了,c++通过jni调用java的非static方法
(2)通过通知(Handler)访问,启动类的非static方法
启动类
public Handler mHandler = new Handler() {//注册通知 public void handleMessage(Message msg) {//等待对方相应这个通知 switch (msg.what) { case CaddCall.SHOW_DIALOG://通知名字 onBuyGasButtonClicked(); //调用非static方法 break; } } };
publicstatic XXX启动类 acty;
在启动类的onCreate 调用 CaddCall.acty = this(告诉CaddCall里的发送通知的方法,给那个类发送通知);
public static final int SHOW_DIALOG = 0x0001; public static APlay acty; private static Handler mHandler;//通知 public static void init(Handler handler) { CaddCall.mHandler = handler; }
public static String googlePlay(final String protuctId, String imagePath) { Message msg = mHandler.obtainMessage();//得到消息 msg.what = CaddCall.SHOW_DIALOG;//得到消息线路 msg.sendToTarget();//发送 return “”; }