现在的位置: 首页 > 综合 > 正文

cocos2d-x与Java层通过Jni进行交互 【Cocos2d-x】cocos2d-x与Java层通过Jni进行交互

2014年11月09日 ⁄ 综合 ⁄ 共 4336字 ⁄ 字号 评论关闭

【Cocos2d-x】cocos2d-x与Java层通过Jni进行交互

分类: cocos2d-x 108人阅读 评论(0) 收藏 举报

今天研究Cocos2d-x,心血来潮的想做个2dx调用Android弹出框的效果.使用jni进行交互有木有.

C++调用Java , Java调用C++.(下载地址瞧下面)

  1. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  2. {  
  3.     JniMethodInfo jmi;  
  4.     if(JniHelper::getStaticMethodInfo(jmi , "com/jni/test/JniTest" , "sayHello" , "([Ljava/lang/String;)V"))  
  5.     {  
  6.         jclass str_cls = jmi.env->FindClass("java/lang/String");  
  7.   
  8.         jstring str1 = jmi.env->NewStringUTF("I'm a titile");  
  9.         jstring str2 = jmi.env->NewStringUTF("Are yor exit game?");  
  10.   
  11.         jobjectArray arrs = jmi.env->NewObjectArray(2 , str_cls , 0);  
  12.         jmi.env->SetObjectArrayElement(arrs , 0 , str1);  
  13.         jmi.env->SetObjectArrayElement(arrs , 1 , str2);  
  14.         jmi.env->CallStaticVoidMethod(jmi.classID , jmi.methodID , arrs);  
  15.     }  
  16. }  
  17.   
  18. extern "C"  
  19. {  
  20.     /* 命名规则:Java_Java的包名_类名*/  
  21.     void Java_com_jni_test_JniTest_sayHello()  
  22.     {  
  23.         CCLog("hello java , i'm c");  
  24.     }  
  25. }  

  1. /**************************************************************************** 
  2. Copyright (c) 2010-2012 cocos2d-x.org 
  3.  
  4. http://www.cocos2d-x.org 
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy 
  7. of this software and associated documentation files (the "Software"), to deal 
  8. in the Software without restriction, including without limitation the rights 
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
  10. copies of the Software, and to permit persons to whom the Software is 
  11. furnished to do so, subject to the following conditions: 
  12.  
  13. The above copyright notice and this permission notice shall be included in 
  14. all copies or substantial portions of the Software. 
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
  22. THE SOFTWARE. 
  23. ****************************************************************************/  
  24. package com.jni.test;  
  25.   
  26. import java.io.UnsupportedEncodingException;  
  27.   
  28. import org.cocos2dx.lib.Cocos2dxActivity;  
  29.   
  30. import android.app.AlertDialog;  
  31. import android.app.Dialog;  
  32. import android.content.DialogInterface;  
  33. import android.os.Bundle;  
  34. import android.os.Handler;  
  35. import android.os.Message;  
  36.   
  37. public class JniTest extends Cocos2dxActivity{  
  38.   
  39.     private static AlertDialog mDialog = null;  
  40.   
  41.     private static Handler mHandler = new Handler(new Handler.Callback() {  
  42.         @Override  
  43.         public boolean handleMessage(Message msg) {  
  44.             String[] str = (String[])msg.obj;  
  45.             mDialog.setTitle(str[0]);  
  46.             mDialog.setMessage(str[1]);  
  47.             mDialog.show();  
  48.             return true;  
  49.         }  
  50.     });  
  51.   
  52.     public static native void sayHello();  
  53.   
  54.     protected void onCreate(Bundle savedInstanceState){  
  55.         super.onCreate(savedInstanceState);  
  56.   
  57.         mDialog = new AlertDialog.Builder(this).create();  
  58.         mDialog.setButton("确定"new AlertDialog.OnClickListener() {  
  59.             @Override public void onClick(DialogInterface dialog, int which) {  
  60.                 JniTest.this.finish();  
  61.             }  
  62.         });  
  63.         mDialog.setButton2("取消"new AlertDialog.OnClickListener() {  
  64.             @Override public void onClick(DialogInterface dialog, int which) {  
  65.   
  66.             }  
  67.         });  
  68.         mDialog.setButton3("HelloC"new AlertDialog.OnClickListener() {  
  69.             @Override public void onClick(DialogInterface dialog, int which) {  
  70.                                 // Java调用c代码  
  71.                 JniTest.this.sayHello();  
  72.             }  
  73.         });  
  74.     }  
  75.   
  76.     static {  
  77.          System.loadLibrary("game");  
  78.     }  
  79.   
  80.     public static void sayHello(String[] str){  
  81.         Message mes = new Message();  
  82.         mes.obj = str;  
  83.         mHandler.sendMessage(mes);  
  84.     }  
  85. }  

效果如下:C++调用Android对话框
Cocos2d-x cocos2d-x与Java层Jni交互 
Java调用C++打印字符串
Cocos2d-x cocos2d-x与Java层Jni交互

代码地址(时间原因,代码未优化,参考下吧...):


更多Cocos2d-x开发博文尽在 Koyoter's Blog

原文标题:【Cocos2d-x】cocos2d-x与Java层通过Jni进行交互

抱歉!评论已关闭.