Cocos2d-xJNI

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

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

  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.     } 
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
	JniMethodInfo jmi;
	if(JniHelper::getStaticMethodInfo(jmi , "com/jni/test/JniTest" , "sayHello" , "([Ljava/lang/String;)V"))
	{
		jclass str_cls = jmi.env->FindClass("java/lang/String");

		jstring str1 = jmi.env->NewStringUTF("I'm a titile");
		jstring str2 = jmi.env->NewStringUTF("Are yor exit game?");

		jobjectArray arrs = jmi.env->NewObjectArray(2 , str_cls , 0);
		jmi.env->SetObjectArrayElement(arrs , 0 , str1);
		jmi.env->SetObjectArrayElement(arrs , 1 , str2);
		jmi.env->CallStaticVoidMethod(jmi.classID , jmi.methodID , arrs);
	}
}

extern "C"
{
	/* 命名规则:Java_Java的包名_类名*/
	void Java_com_jni_test_JniTest_sayHello()
	{
		CCLog("hello java , i'm c");
	}
}

  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 native void 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 public void 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 public void 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 static void sayHello(String[] str){ 
  77.         Message mes = new Message(); 
  78.         mes.obj = str; 
  79.         mHandler.sendMessage(mes); 
  80.     } 
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org

http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package com.jni.test;

import java.io.UnsupportedEncodingException;

import org.cocos2dx.lib.Cocos2dxActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class JniTest extends Cocos2dxActivity{

	private static AlertDialog mDialog = null;

	private static Handler mHandler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			String[] str = (String[])msg.obj;
			mDialog.setTitle(str[0]);
			mDialog.setMessage(str[1]);
	    	mDialog.show();
			return true;
		}
	});

	public static native void sayHello();

	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);

		mDialog = new AlertDialog.Builder(this).create();
		mDialog.setButton("确定", new AlertDialog.OnClickListener() {
			@Override public void onClick(DialogInterface dialog, int which) {
				JniTest.this.finish();
			}
		});
		mDialog.setButton2("取消", new AlertDialog.OnClickListener() {
			@Override public void onClick(DialogInterface dialog, int which) {

			}
		});
		mDialog.setButton3("HelloC", new AlertDialog.OnClickListener() {
			@Override public void onClick(DialogInterface dialog, int which) {
                                // Java调用c代码
				JniTest.this.sayHello();
			}
		});
	}

    static {
         System.loadLibrary("game");
    }

    public static void sayHello(String[] str){
    	Message mes = new Message();
    	mes.obj = str;
    	mHandler.sendMessage(mes);
    }
}

效果如下: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进行交互

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值