cocos2dx 调用java层代码
1.导入头文件
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include <jni.h> #include "android/log.h" #include "platform/android/jni/JniHelper.h" #endif
2.详尽代码
JniMethodInfo minfo;//JniHelper /* 测试用方法 */ /*bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/cocoa/HiWorld","loginGree", "()V"); // if (isHave) { //CCLog("有showText "); minfo.env -> CallStaticVoidMethod(minfo.classID,minfo.methodID); }else { //CCLog("没有方法showText"); }*/ /* 分享 */ /*//将c++中的string转换成java中的string //char str[] = "test"; bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/cocoa/HiWorld","shareSina", "(Ljava/lang/String;Ljava/lang/String;)V"); // if (isHave) { //CCLog("有share "); jstring jstr = minfo.env->NewStringUTF("test1 share"); jstring jst = minfo.env->NewStringUTF("/data/data/com.cocoa/cy.png"); //jstring jst = minfo.env->NewStringUTF(""); minfo.env -> CallStaticVoidMethod(minfo.classID,minfo.methodID,jstr,jst); }else { //CCLog("没有方法share"); }*/ /* 设置高分 */ /*jint ind = 0; jlong lsre = 2202l; bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/cocoa/HiWorld","setHighScore", "(IJ)V"); if (isHave) { minfo.env -> CallStaticVoidMethod(minfo.classID,minfo.methodID,ind,lsre); }*/ /* 成就解锁 */ /*jint aind = 0; bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/cocoa/HiWorld","unLock", "(I)V"); if (isHave) { minfo.env -> CallStaticVoidMethod(minfo.classID,minfo.methodID,aind); }*/ /* 测试用方法 */ bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/cocoa/HiWorld","rtnActivity","()Ljava/lang/Object;"); jobject jobj; if (isHave) { jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); } //CCLog(" jobj存在"); /* 测试用方法,非静态无参数无返回值方法 */ /*isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","showText", "()V"); if (isHave) { minfo.env -> CallVoidMethod(jobj,minfo.methodID); }*/ /* 测试用方法,非静态有java类型的String参数无返回值方法 */ /*isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","showText", "(Ljava/lang/String;)V"); if (isHave) { jstring jmsg = minfo.env->NewStringUTF("msg okey!"); minfo.env -> CallVoidMethod(jobj,minfo.methodID,jmsg); }*/ /* 测试用方法,返回java类型的String,有java类型的String和int参数方法 */ /*isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","showText", "(Ljava/lang/String;I)Ljava/lang/String;"); if (isHave) { jstring jmsg = minfo.env->NewStringUTF("msg okey! return string"); jint index = 0; minfo.env -> CallObjectMethod(jobj,minfo.methodID,jmsg,index); }*/ /* 测试用方法,返回java类型的String[],有java类型的String[]和int参数方法 */ /*isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","showText", "([Ljava/lang/String;I)[Ljava/lang/String;"); if (isHave) { jobjectArray args = 0; jstring str; jsize len = 5; const char* sa[] = {"Hi,","World!","JNI ","is ","fun"}; int i = 0; args = minfo.env->NewObjectArray(len,minfo.env->FindClass("java/lang/String"),0); for(i=0;iNewStringUTF(sa[i]); minfo.env->SetObjectArrayElement(args,i,str); } //minfo.env->GetStringArrayRegion(args,0,10,buf); //jintArray jmsg = {1,2,3}; //minfo.env->NewStringUTF("msg okey! return string"); jint index = 0; minfo.env -> CallObjectMethod(jobj,minfo.methodID,args,index); }*/ /* 测试用方法,无返回类型,有java类型的int[]和int参数方法 */ /*isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","testArr", "([II)V"); if (isHave) { jint buf[]={7,5,8,9,3}; jintArray jintArr; //定义jint数组 jintArr = minfo.env->NewIntArray(5); minfo.env->SetIntArrayRegion(jintArr,0,5,buf); jint index = 0; minfo.env -> CallVoidMethod(jobj,minfo.methodID,jintArr,index); }*/ /* 测试用方法,无返回类型,有java类型的byte[]和int参数方法 */ isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","testArr", "([BI)V"); if (isHave) { jbyte buf[]={7,5,8,9,3}; jbyteArray jbyteArr; //定义jbyte数组 jbyteArr = minfo.env->NewByteArray(5); minfo.env->SetByteArrayRegion(jbyteArr,0,5,buf); jint index = 0; minfo.env -> CallVoidMethod(jobj,minfo.methodID,jbyteArr,index); }
private static HiWorld hiWorld = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); hiWorld = this; if (detectOpenGLES20()) { // get the packageName,it's used to set the resource path String packageName = getApplication().getPackageName(); super.setPackageName(packageName); // set content setContentView(R.layout.game_demo); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title); mGLView = (Cocos2dxGLSurfaceView) findViewById(R.id.game_gl_surfaceview); mGLView.setTextField((Cocos2dxEditText) findViewById(R.id.textField)); mGLView.setEGLContextClientVersion(2); mGLView.setCocos2dxRenderer(new Cocos2dxRenderer()); task = new TimerTask() { @Override public void run() { // HiWorld.shoot(hiWorld); Log.e("-------------------", "-------------------"); // 调用c++中的方法 System.out.println("------------------------" + stringZjy1()); } }; timer = new Timer(); timer.schedule(task, 5000); } else { Log.d("activity", "don't support gles2.0"); finish(); } static { System.loadLibrary("game"); } // c++中調用的方法 public static Object rtnActivity() { System.out.println("----------rtnActivity"); return hiWorld; } // c++中調用的方法,传String类型 public void showText(final String msg) { // 添加到主线程 hiWorld.runOnUiThread(new Runnable() { public void run() { System.out.println("----------msg:"+msg); } }); } //c++中調用的方法,传String类型和int类型 public String showText(final String msg,final int index) { // 添加到主线程 hiWorld.runOnUiThread(new Runnable() { public void run() { System.out.println("----------msg:"+msg+"; index="+index); } }); return "okey String showText(final String msg,final int index)"; } //c++中調用的方法,传String[]类型和int类型 public String[] showText(final String[] msg,final int index) { String[] strArr = {"1","2","3","4","5"}; // 添加到主线程 hiWorld.runOnUiThread(new Runnable() { public void run() { for(String _str:msg){ System.out.println("----------String[] msg:"+_str+"; index="+index); } } }); return strArr; } //c++中調用的方法,传int[]类型和int类型 public void testArr(final int msg[],final int index) { // 添加到主线程 hiWorld.runOnUiThread(new Runnable() { public void run() { System.out.println("----------int[] msg len:"+msg.length); for(int _bl:msg){ System.out.println("----------int[] msg:"+_bl+"; index="+index); } } }); } //c++中調用的方法,传int[]类型和int类型 public void testArr(final byte msg[],final int index) { // 添加到主线程 hiWorld.runOnUiThread(new Runnable() { public void run() { System.out.println("----------byte[] msg len:"+msg.length); for(int _bl:msg){ System.out.println("----------byte[] msg:"+_bl+"; index="+index); } } }); }
调用非静态方法
JniMethodInfo minfo;//定义Jni函数信息结构体 //getStaticMethodInfo 次函数返回一个bool值表示是否找到此函数 bool isHave = JniHelper::getStaticMethodInfo(minfo, "com/cocos/animation/animation", //类的路径 "rtnActivity", //方法名 "()Ljava/lang/Object;"); //括号里的是参数,后面的是返回值。 jobject jobj; if (isHave) { jobj =minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); CCLog("jobj :%x",jobj); } CCLog("正确获取到 jobj"); // isHave = JniHelper::getMethodInfo(minfo, "com/cocos/animation/animation", //类的路径 "nostaticFun", //方法名 "(Ljava/lang/String;)V"); //括号里的是参数,后面的是返回值。 if (isHave) { jstring jmsg=minfo.env->NewStringUTF("伊莎贝拉 SB鑫,Sb Sb 还是SB"); minfo.env->CallVoidMethod(jobj, minfo.methodID,jmsg); } CCLog("jni-java函数执行完毕");
java层代码被调用函数
//---- 函数示例之非静态函数调用 //(先获取个对象) public static Activity actInstance=new animation();//定义单例 public static Object rtnActivity() { return actInstance; } //使用此对象进行调用非静态函数 public void nostaticFun(String str) { Log.e("Himi", "no static Function is OK - By Himi:str="+str); StatusesAPI api=new StatusesAPI(animation.accessToken); /*api.update(str, "90.00","90.00",new RequestListener() { @Override public void onIOException(IOException arg0) { // TODO Auto-generated method stub } @Override public void onError(WeiboException arg0) { // TODO Auto-generated method stub } @Override public void onComplete(String arg0) { // TODO Auto-generated method stub // Toast.makeText(animation.this, "分享成功", Toast.LENGTH_SHORT).show(); } });*/ api.upload(str,"/sdcard/33.jpg", "90.00", "90.00", new RequestListener() { @Override public void onIOException(IOException arg0) { // TODO Auto-generated method stub } @Override public void onError(WeiboException arg0) { // TODO Auto-generated method stub } @Override public void onComplete(String arg0) { // TODO Auto-generated method stub // Toast.makeText(animation.this, "分享图片成功", Toast.LENGTH_SHORT).show(); } }); }
转载至:http://go3k.org/?p=49
http://www.himigame.com/android-game/725.html
相关推荐
LeoDLi 2020-07-19
linxinfei 2013-08-28
wsmrcool 2015-07-10
shawjan 2016-01-27
shawjan 2016-01-22
Tommy凌飞 2019-06-20
86540698 2019-06-20
kuibuzhiqianli 2016-02-17
mnbvcxz 2016-01-27
LibraXin 2015-07-10
liuwentao 2015-05-22
shawjan 2014-12-16
Tommy凌飞 2014-04-28
shawjan 2013-08-28
WintonTalks 2013-05-17
shawjan 2013-05-13