Cocos2d-xJNI

来源:互联网 发布:网络变压器接线图 编辑:程序博客网 时间:2024/06/03 20:29
Cocos2d-xJNI

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

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

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

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

效果如下: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进行交互
原文地址:http://acoder.me/cocos2d-x-cocos-and-java-chat.html

0 0
原创粉丝点击