.cpp
//加载头文件
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "../android/jni/JniHelper.h"
#include <android/log.h>
#endif
.cpp中调用java函数,"(I)Ljava/lang/String;" 要注意String后面的“;”号一定要加上
//非静态函数示例
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
JniMethodInfo method;
bool b = JniHelper::getStaticMethodInfo(method, "com.cocos2dx.MyTest.javas", "add", "(II)I");
if(b) {
jint square = method.env->CallStaticIntMethod(method.classID, method.methodID, 50, 60);
CCLog("square=%d",square);
}
CCLog("jni-java函数执行完毕");
#endif
CCLog("jni");
}
break;
case 1:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
JniMethodInfo method;
bool b = JniHelper::getStaticMethodInfo(method, "com.cocos2dx.MyTest.javas", "sub", "(II)I");
if(b) {
jint square = method.env->CallStaticIntMethod(method.classID, method.methodID, 90, 60);
CCLog("square=%d",square);
}
CCLog("jni-java函数执行完毕");
//静态函数示例1.无参数,无返回值
b=JniHelper::getStaticMethodInfo(method,"com.cocos2dx.MyTest.javas","testFun","()V");
if (b)
{
method.env->CallStaticIntMethod(method.classID,method.methodID);
}
//静态函数示例2.有参数,无返回值
b=JniHelper::getStaticMethodInfo(method,"com.cocos2dx.MyTest.javas","testFunWithInt","(I)V");
if (b)
{
method.env->CallStaticIntMethod(method.classID,method.methodID,100);
}
//静态函数示例3.有参数,有返回值int
b=JniHelper::getStaticMethodInfo(method,"com.cocos2dx.MyTest.javas","testFunWithIntAndRtn","(I)I");
if (b)
{
jint _int=method.env->CallStaticIntMethod(method.classID,method.methodID,200);
CCLog("cocos2d _int=%d",_int);
//尝试jint是否能正常接收返回的int值
b=JniHelper::getStaticMethodInfo(method,"com.cocos2dx.MyTest.javas","testFunWithInt","(I)V");
if (b)
{
method.env->CallStaticIntMethod(method.classID,method.methodID,_int);
}
}
//静态函数示例4.有参数,有返回值(string类型)
b=JniHelper::getStaticMethodInfo(method,"com.cocos2dx.MyTest.javas","testFunWithStringAndRtn","(I)Ljava/lang/String;");
if (b)
{
jstring jstr=(jstring)method.env->CallStaticIntMethod(method.classID,method.methodID,100);
std::string str = JniHelper::jstring2string(jstr);
CCLog("jni-java函数执行完毕,得到的string:%s",str.c_str());
}
//非静态函数示例
JniMethodInfo minfo;//定义Jni函数信息结构体
//getStaticMethodInfo 次函数返回一个bool值表示是否找到此函数
bool isHave = JniHelper::getStaticMethodInfo(minfo,
"com.cocos2dx.MyTest.javas", //类的路径
"rtnActivity", //方法名
"()Ljava/lang/Object;"); //括号里的是参数,后面的是返回值。
jobject jobj;
if (isHave) {
jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);
}
CCLog("正确获取到 jobj");
//
isHave = JniHelper::getMethodInfo(minfo,
"com.cocos2dx.MyTest.javas", //类的路径
"nostaticFun", //方法名
"()V"); //括号里的是参数,后面的是返回值。
if (isHave) {
minfo.env->CallVoidMethod(jobj, minfo.methodID);
}
#endif
java代码
public static Activity actInstance;//定义单例
public static Object rtnActivity() {
Log.e("Himi", "获取对象");
return actInstance;
}
protected void onCreate(){
//super.onCreate(savedInstanceState);
actInstance=this;
Log.e("Himi", "OnCreat");
}
public static int add(int a, int b) {
int c=a+b;
Log.e("Himi", "静态函数示例1.有参数,无返回值;传入的参数int=" + c);
return a+b;
}
public static int sub(int a,int b)
{
int c=a-b;
Log.e("Himi", "静态函数示例1.sub int=" + c);
return c;
}
public boolean ju(boolean b) {
return !b;
}
// 静态函数示例1.无参数,无返回值
public static void testFun() {
Log.e("Himi", "静态函数示例1.无参数,无返回值");
}
// 静态函数示例2.有参数,无返回值
public static void testFunWithInt(int _int) {
Log.e("Himi", "静态函数示例1.有参数,无返回值;传入的参数int=" + _int);
}
// 静态函数示例3.有参数,有返回值(int)
public static int testFunWithIntAndRtn(int _int) {
Log.e("Himi", "静态函数示例1.有参数,有返回值;传入的参数int=" + _int);
return _int+1000;
}
// 静态函数示例4.有参数,有返回值(String类型)
public static String testFunWithStringAndRtn(int _int) {
Log.e("Himi", "静态函数示例4.有参数,有返回值(String类型);int=" + _int);
return "yes,return 'String' is OK --by Himi";
}
//---- 函数示例之非静态函数调用
//使用此对象进行调用非静态函数
public void nostaticFun() {
Log.e("Himi", "no static Function is OK - By Himi");
}
本文中静态函数获取会出错,暂时还没解决,如果测试请注销该部分代码。
原文:http://xiaominghimi.blog.51cto.com/2614927/908818