cocos2dx2.3 Java和C++通信

参数类型 对应简写

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);
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值