纳金网
标题:
JniHepleer——实现C++调用Java代码
[打印本页]
作者:
王者再临
时间:
2014-8-30 01:41
标题:
JniHepleer——实现C++调用Java代码
①加入头文件
将Jdk中的"include/jni.h"和“include/win32/jni_md.h”拷贝到VS201X安装目录的"VC/include"下
②包含头文件
#include "../android/jni/JniHelper.h"
#include "jni.h"
③C++中代码(主要的)
//jni调用测试
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo info;
//bool isHave = JniHelper::getStaticMethodInfo(info, "com/lch/myGame", "function", "()V"); //()中为参数类型,V为返回值类型
//bool isHave = JniHelper::getStaticMethodInfo(info, "com/lch/myGame", "function1", "(Ljava/lang/String;)V");
//bool isHave = JniHelper::getStaticMethodInfo(info, "com/lch/myGame", "function2", "()Ljava/lang/String;");
//bool isHave = JniHelper::getStaticMethodInfo(info, "com/lch/myGame", "function3", "(II)I");
bool isHave = JniHelper::getStaticMethodInfo(info, "com/lch/myGame", "getObject", "()Ljava/lang/Object;");
jobject jobj;
if (isHave)
{
CCLog("C++ : The function is exist");
//无参数无返回值
//info.env->CallStaticVoidMethod(info.classID, info.methodID);
//有参数无返回值
/*jstring str = info.env->NewStringUTF("msg");
info.env->CallStaticVoidMethod(info.classID, info.methodID, str);*/
//无参数有返回值
/*jstring jstr = (jstring)info.env->CallStaticObjectMethod(info.classID, info.methodID);
std::string str = JniHelper::jstring2string(jstr);
CCLog("C++ : %s", str.c_str());*/
//有参数有返回值
/*jint x = 10;
jint y = 20;
jint jRes = info.env->CallStaticIntMethod(info.classID, info.methodID, x, y);
CCLog("C++ : result: %d", jRes);*/
//调用动态方法步骤一
jobj = info.env->CallStaticObjectMethod(info.classID, info.methodID);
}
else
{
CCLog("C++ : The function is not exist");
}
isHave = JniHelper::getMethodInfo(info, "com/lch/myGame", "function4", "()V");
if (isHave)
{
CCLog("C++ : The function is exist");
//调用动态方法步骤二
info.env->CallVoidMethod(jobj, info.methodID);
}
else
{
CCLog("C++ : The function is not exist");
}
#endif
④Java中代码(主要的)
public static myGame instance;
public static Handler handler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
// 退出游戏
Builder builder = new Builder(FlyGame.this);
builder.setTitle("亲!确定要退出游戏吗?")
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
System.exit(0);
}
}).setNegativeButton("取消", null).show();
default:
break;
}
}
};
}
public static void function(){
System.out.println("Java: " +"无参无返回值");
}
public static void function1(String str){
System.out.println("Java: " +"有参无返回值 " + str);
}
public static String function2(){
System.out.println("Java: " +"无参有返回值 ");
return "Java return msg";
}
public static int function3(int x, int y){
System.out.println("Java: " +"有参有返回值 ");
return x + y;
}
public static Object getObject(){
System.out.println("Java: " +"返回一个实例 ");
return instance;
}
public void function4(){
System.out.println("Java: " +"非静态函数 ");
buyProp();
}
public static void exitGame(){
Message msg = Message.obtain();
msg.what = 1;
handler.sendMessage(msg);
System.out.println("Java: " +"退出游戏确认 ");
}
作者:
hyui
时间:
2014-8-30 17:50
Good to know !
欢迎光临 纳金网 (http://rs.narkii.com/club/)
Powered by Discuz! X2.5