转载请注明,原文链接:http://www.cnblogs.com/hewei2012/p/3376616.html
前提条件:
1.操作的游戏工程和cocos2d_x游戏引擎是一个目录的
2.跟jni相关的C++代码文件放在proj.android\jni\hellocpp目录下,每加一个cpp文件,都需在proj.android\jni的Andriod.mk文件中添加:
LOCAL_SRC_FILES := hellocpp/main.cpp \ hellocpp/test.cpp \ <--为新添加的 ...
3.跟jni相关的java代码文件放在proj.android\src\jt\SwordsMen目录下
一、C++调用Andriod接口
1.定义一个.h文件“test.h”,内容:
extern "C" //因为jni将java代码转过来是c的,所以C++引用得加上 { void showExitPt(const char *title, const char *msg); //showExitPt将调用Andriod工程中实现的方法 }
2.定义一个.cpp文件“test.cpp”,内容:
#include <jni.h> #include "platform/android/jni/JniHelper.h" ... //表示proj.android\src\jt\SwordsMen\JniTestHelper.java的,在该文件里有showTipDialog的实现 #define CLASS_NAME "jt/SwordsMen/JniTestHelper"
//事实上,showExitPt一般都是跟showTipDialog同名的,这里为了区分,写成不一样 void showExitPt(const char *title, const char *msg)
{
JniMethodInfo t;
//getStaticMethodInfo判断是否在java中实现了名字showTipDialog的方法
//"(Ljava/lang/String;Ljava/lang/String;)V" 对该方法的一个描述,详见说明
if(JniHelper::getStaticMethodInfo(t, CLASS_NAME, "showTipDialog", "(Ljava/lang/String;Ljava/lang/String;)V"))
{
jstring jTitle = t.env->NewStringUTF(title);
jstring jMsg = t.env->NewStringUTF(msg);
//根据该方法的返回值调用对应的CallStaticxxxMethod方法,如CallStaticIntMethod
t.env->CallStaticVoidMethod(t.classID, t.methodID, jTitle, jMsg);
t.env->DeleteLocalRef(jTitle);
t.env->DeleteLocalRef(jMsg);
}
}
3.java实现,代码如下,具体代码最后一定提供
定义一个JniTestHelper.java文件,内容:
private static void showTipDialog(final String title, final String text) { Message msg = mHandler.obtainMessage(); msg.what = SwordsMen.SHOW_DIALOG; DialogMessage dm = new DialogMessage(); dm.title = title; dm.msg = text; msg.obj = dm; msg.sendToTarget(); }
4.程序使用
//需要宏处理,否则so文件生成会有问题 #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "../proj.android/jni/hellocpp/test.h" #endif void CMainScene::exitGame() { #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) showExitPt("Prompt", "if Exit Game ?"); #endif #if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) ExitDlg *pDlg = ExitDlg::create(); addChild(pDlg, 2); #endif }
二、Andriod调用C++接口
1.在test.cpp中加入实现内容:
#include "cocos2d.h" void setPackageName(const char *packageName) { CCLog("packageName: %s", packageName); } void exitApp() { CCDirector::sharedDirector()->end(); }
2.Andriod工程中的声明(JniTestHelper.java):
public static native void setPackageName(String packageName); public static native void exitApp();
3.将C++实现与Andriod工程中的声明关联(test.cpp)
//1.Java_:是格式,必须加的
//2.jt_SwordsMen_JniTestHelper:是路径proj.android\src\jt\SwordsMen\JniTestHelper.java去掉proj.andriod\src的部分
//3.setPackageName:是Andriod工程中声明的名字
//4.中间需要_分开
void Java_jt_SwordsMen_JniTestHelper_setPackageName(JNIEnv *env, jobject thiz, jstring packageName)
{
const char *pkgName = env->GetStringUTFChars(packageName, NULL);
setPackageName(pkgName);
env->ReleaseStringUTFChars(packageName, pkgName);
}
void Java_jt_SwordsMen_JniTestHelper_exitApp(JNIEnv *env, jobject thiz)
{
exitApp();
}
4.程序调用:
JniTestHelper.exitApp(); JniTestHelper.setPackageName(this.getPackageName());
例子:
.h:
#pragma once
#include "cocos2d.h"
USING_NS_CC;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxHelper"
#define SDKFUNCITION(__FUNCNAME__) Java_jt_SwordsMen_JniTestHelper_##__FUNCNAME__
#endif
class Vibrator
{
public:
static void vibrate(long time);
static void cancelVibrate();
static void JaveUseC();
static void JaveUseC123(const char *packageName);
};
.cpp:
#include "Vibrator.h"
void Vibrator::vibrate(long time)
{
//调用java中的vibrate接口
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "vibrate", "(I)V"))
{
t.env->CallStaticVoidMethod(t.classID, t.methodID, time);
t.env->DeleteLocalRef(t.classID);
}
#endif
}
void Vibrator::cancelVibrate()
{
//调用java中的cancelVibrate接口
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "cancelVibrate", "()V"))
{
t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
#endif
}
#if CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID
//java调用C++ 注册函数接口给JAVA调用
extern "C" {
JNIEXPORT void JNICALL SDKFUNCITION(JaveUseC)(JNIEnv* env, jobject thiz, jstring text) {
const char* pszText = env->GetStringUTFChars(text, NULL);
//SDKManager::getInstance()->buyOrderCallback(pszText);
JaveUseC();
env->ReleaseStringUTFChars(text, pszText);
}
JNIEXPORT void JNICALL SDKFUNCITION(JaveUseC123)(JNIEnv* env, jobject thiz, jstring text) {
const char* pszText = env->GetStringUTFChars(text, NULL);
//SDKManager::getInstance()->buyOrderCallback(pszText);
JaveUseC123();
env->ReleaseStringUTFChars(text, pszText);
}
}
#endif
void Vibrator::JaveUseC()
{
CCLog("Vibrator::JaveUseC");
}
void Vibrator::JaveUseC123(const char *packageName)
{
CCLog("Vibrator::JaveUseC123");
}