参数类型 对应简写
boolean Z
byte B
char C
short S
int I
long L
void V
Object Ljava/lang/Object;
String Ljava/lang/String;
下面我C++调用java方法传了3个参数给java.
#include "HelloWorldScene.h"
USING_NS_CC;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#define JAVA_CLASS "com.hj.sdktest.Helper"
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
extern "C"{
//在这里可以调用C++里的方法
JNIEXPORT void JNICALL Java_com_hj_sdktest_Helper_tocpp(JNIEnv* env,jobject thiz,jstring paycode,jint num,jstring paycode2)
{
const char *_paycode= env->GetStringUTFChars(paycode, NULL);
CCLog("num===%d",num);
CCMessageBox("successful","tishi");
}
};
#endif
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
/
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
CCMenuItemImage *pCloseItem2 = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback2));
pCloseItem2->setPosition(ccp(visibleSize.width/2 ,visibleSize.height/2));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem,pCloseItem2, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
/
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}
void HelloWorld::menuCloseCallback2( CCObject* pSender )
{
std::string str = "001";
int num = 1;
std::string str2 = "666";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo minfo;
//有参数,无返回值==============
bool isHave = JniHelper::getStaticMethodInfo(minfo, JAVA_CLASS , "mmPay" , "(Ljava/lang/String;ILjava/lang/String;)V" );
if (isHave) {
jstring jStr = minfo.env->NewStringUTF(str.c_str());
jstring jStr2 = minfo.env->NewStringUTF(str2.c_str());
minfo.env->CallStaticVoidMethod(minfo.classID,minfo.methodID,jStr,num,jStr2);
minfo.env->DeleteLocalRef(minfo.classID);
minfo.env->DeleteLocalRef(jStr);
minfo.env->DeleteLocalRef(jStr2);
}
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}
然后是java方法:包名:package com.hj.sdktest;
package com.hj.sdktest;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import android.os.Bundle;
public class SDKTest extends Cocos2dxActivity{
private static SDKTest _instance = null;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
_instance = this;
}
public static SDKTest getInstance(){
return _instance;
}
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
// SDKTest should create stencil buffer
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
return glSurfaceView;
}
static {
System.loadLibrary("cocos2dcpp");
}
public void function(String paycode,int num,String paycode2){
android.util.Log.i("=======paycode2"+paycode2 , "paycode2");
Helper.getInstance().tocpp(paycode,num,paycode2); //==============购买成功
}
}
======Helper类:
package com.hj.sdktest;
import android.util.Log;
import org.cocos2dx.*;
public class Helper {
public static Helper _instance = null;
public static Helper getInstance(){
if (null == _instance){
_instance = new Helper();
}
return _instance;
}
//返回给C++
public static native void tocpp(String paycode,int num,String paycode2);
//C++调用付费接口
public static void mmPay(String paycode,int num,String paycode2){
Log.i("paycode ="+paycode, "paycode");
SDKTest.getInstance().function(paycode,num,paycode2);
}
}