Cocos2d-xJNI
来源:互联网 发布:网络变压器接线图 编辑:程序博客网 时间:2024/06/03 20:29
Cocos2d-xJNI
今天研究Cocos2d-x,心血来潮的想做个2dx调用Android弹出框的效果.使用jni进行交互有木有.
C++调用Java , Java调用C++.(下载地址瞧下面)
- 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");
- }
- }
- /****************************************************************************
- 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 nativevoid sayHello();
-
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
-
- mDialog = new AlertDialog.Builder(this).create();
- mDialog.setButton("确定", new AlertDialog.OnClickListener() {
- @Override publicvoid 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 publicvoid onClick(DialogInterface dialog, int which) {
- // Java调用c代码
- JniTest.this.sayHello();
- }
- });
- }
-
- static {
- System.loadLibrary("game");
- }
-
- public staticvoid sayHello(String[] str){
- Message mes = new Message();
- mes.obj = str;
- mHandler.sendMessage(mes);
- }
- }
效果如下:C++调用Android对话框
Java调用C++打印字符串
代码地址(时间原因,代码未优化,参考下吧...):
点击下载示例代码
更多Cocos2d-x开发博文尽在Koyoter's Blog原文标题:【Cocos2d-x】cocos2d-x与Java层通过Jni进行交互原文地址:http://acoder.me/cocos2d-x-cocos-and-java-chat.html
今天研究Cocos2d-x,心血来潮的想做个2dx调用Android弹出框的效果.使用jni进行交互有木有.
C++调用Java , Java调用C++.(下载地址瞧下面)
- 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");
- }
- }
- /****************************************************************************
- 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 nativevoid sayHello();
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- mDialog = new AlertDialog.Builder(this).create();
- mDialog.setButton("确定", new AlertDialog.OnClickListener() {
- @Override publicvoid 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 publicvoid onClick(DialogInterface dialog, int which) {
- // Java调用c代码
- JniTest.this.sayHello();
- }
- });
- }
- static {
- System.loadLibrary("game");
- }
- public staticvoid sayHello(String[] str){
- Message mes = new Message();
- mes.obj = str;
- mHandler.sendMessage(mes);
- }
- }
效果如下:C++调用Android对话框
Java调用C++打印字符串
代码地址(时间原因,代码未优化,参考下吧...):
点击下载示例代码
0 0
- Cocos2d-xJNI
- cocos2d
- Cocos2D
- cocos2d
- cocos2d
- cocos2d
- cocos2d
- cocos2d
- Cocos2d
- cocos2d
- cocos2d
- cocos2d
- cocos2d
- cocos2d
- cocos2d
- cocos2d
- cocos2d
- cocos2d
- 韩庚要上太快?
- Linux Shell 之 $参数
- 深入理解JDBC的超时设置
- 浅谈MySql的存储引擎(表类型)
- python datetime模块
- Cocos2d-xJNI
- unix下C标准文件操作及进程相关知识
- OCP-1Z0-053-V13.02-681题
- 手动向maven本地库添加jar包 验证好用 转
- void value not ignored as it ought to be
- 国内阉割版安卓手机无法使用google maps
- 主键和索引的创建,增加各种约束
- Python对C 语言的调用
- Linux下用fcrackzip破解zip密码