1.HelloCpp.java

 /****************************************************************************
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 org.cocos2dx.hellocpp; import org.cocos2dx.lib.Cocos2dxActivity; import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message; public class HelloCpp extends Cocos2dxActivity{
static
{
System.loadLibrary("hellocpp");
}
//java调用c++
public static native void javaToC(); private static AlertDialog mDialog = null; 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) {
HelloCpp.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代码
HelloCpp.this.javaToC();
}
}); } //c++会传入string[]然后调用c++此方法 此方法再调用下面handler方法
public static void sayHelloCToJava(String[] str){
Message mes = new Message();
mes.obj = str;
mHandler.sendMessage(mes);
} 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;
}
});
}

2.main.cpp

#include "AppDelegate.h"
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#include <android/log.h>
#include "cocos2d.h" #define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) using namespace cocos2d; extern "C"
{ jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
JniHelper::setJavaVM(vm); return JNI_VERSION_1_4;
}
//java native 调用c++的方法
/* 命名规则:Java_Java的包名_类名*/
void Java_org_cocos2dx_hellocpp_HelloCpp_javaToC()
{
CCLog("hello java , i'm c");
} void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)
{
if (!CCDirector::sharedDirector()->getOpenGLView())
{
CCEGLView *view = CCEGLView::sharedOpenGLView();
view->setFrameSize(w, h); AppDelegate *pAppDelegate = new AppDelegate();
CCApplication::sharedApplication()->run();
}
else
{
ccGLInvalidateStateCache();
CCShaderCache::sharedShaderCache()->reloadDefaultShaders();
ccDrawInit();
CCTextureCache::reloadAllTextures();
CCNotificationCenter::sharedNotificationCenter()->postNotification(EVENT_COME_TO_FOREGROUND, NULL);
CCDirector::sharedDirector()->setGLDefaultValues();
} #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
// typedef struct JniMethodInfo_
// {
// JNIEnv * env;
// jclass classID;
// jmethodID methodID;
// } JniMethodInfo; //c++调用java包org/cocos2dx/hellocpp/HelloCpp类中的sayHelloCToJava()方法
JniMethodInfo info;
if(JniHelper::getStaticMethodInfo(info , "org/cocos2dx/hellocpp/HelloCpp" , "sayHelloCToJava" , "([Ljava/lang/String;)V"))
{
jclass str_cls = info.env->FindClass("java/lang/String");
jstring str1 = info.env->NewStringUTF("I'm a titile");
jstring str2 = info.env->NewStringUTF("this is the info");
jobjectArray arrs = info.env->NewObjectArray( , str_cls , );
info.env->SetObjectArrayElement(arrs , , str1);
info.env->SetObjectArrayElement(arrs , , str2);
info.env->CallStaticVoidMethod(info.classID , info.methodID , arrs);
}
#endif
} }

效果图

同时后台会打印

http://www.it165.net/pro/html/201305/5914.html

http://www.zaojiahua.com/using-jni.html

cocos2d-x 2.2.6中c++通过JNI与java互调的更多相关文章

  1. [转]在 Eclipse 中嵌入 NASA World Wind Java SDK

    使用此开源 SDK 开发 GIS 应用程序 NASA 开发的开源 World Wind Java (WWJ) SDK 为地理信息系统(Geographic Information Systems,GI ...

  2. HAL中通过JNI调用java方法【转】

    转载请注明本文出处:http://www.cnblogs.com/xl19862005 作者:Xandy 由于工作的需要,最近一直在研究HAL.JNI.Java方法之间互调的问题,并做了如下一些记录和 ...

  3. 1、c#中可以有静态构造方法,而java中没有,例如在单例模式中c#可以直接在静态构造中实例化对象,而java不可以

    1.c#中可以有静态构造方法,而java中没有,例如在单例模式中c#可以直接在静态构造中实例化对象,而java不可以

  4. 在WebView中如何让JS与Java安全地互相调用

    在现在安卓应用原生开发中,为了追求开发的效率以及移植的便利性,使用WebView作为业务内容展示与交互的主要载体是个不错的折中方案.那么在这种Hybrid(混合式) App中,难免就会遇到页面JS需要 ...

  5. idea中编译项目报错 java: javacTask: 源版本 1.8 需要目标版本 1.8

    问题如上面所叙: > idea中编译项目报错 java: javacTask: 源版本 1.8 需要目标版本 1.8 解决方案: > Setting->Compiler->Ja ...

  6. Android NDK开发之从Java与C互调中详解JNI使用(一)

    生活 这一个礼拜过得真的是苦不堪言,上周因为打球脚踝直接扭伤,肿的想猪蹄一样,然后休息几天消肿了,可以缓慢龟速的行走了,然而五一回来上班第一天,上班鞋子还能穿上,下班脚已插不进鞋子里面了,好吧,又肿回 ...

  7. studio中碰到的jni问题:java.lang.UnsatisfiedLinkError

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52606328 studio中碰到 ...

  8. 定位现网环境中最耗费CPU的Java线程

    参考:JVM性能调优监控工具jps.jstack.jmap.jhat.jstat.hprof使用详解 下面通过一个实例找出某个Java进程中最耗费CPU的Java线程并定位堆栈信息,用到的命令有ps. ...

  9. 在Eclipse中运行Jboss时出现java.lang.OutOfMemoryError:PermGen space及其解决方法

    在Eclipse中运行Jboss时出现java.lang.OutOfMemoryError:PermGen space及其解决方法 在Eclipse中运行Jboss时,时间太长可能有时候会出现java ...

随机推荐

  1. View not attached to window manager

    java.lang.IllegalArgumentException: View not attached to window manager 在用ProgressDialog的时候,任务结束后Dis ...

  2. kafka的一些常用命令

    启动zookeeper bin/zookeeper-server-start.sh config/zookeeper.properties & 启动kafka bin/kafka-server ...

  3. ios swift(1)冒泡排序

    //冒泡排序  稳定性最高  时间复杂度高 O(n(2)) ,交换次数太多, 一次交换等于三次赋值    最简单 var count = 0 func sortInts(inout data : [I ...

  4. poj 2479 dp求分段最大和

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38079   Accepted: 11904 Des ...

  5. mac qq截图功能失效后,如何重启截图功能?

    在finder中打开应用程序目录,找到QQ,右键单击QQ,选择显示包内容,此时会打开一个文件夹. 进入以下路径Library/LoginItems然后双击ScreenCapture这个进程,截图功能即 ...

  6. Win7中,取消共享文件夹后有个小锁

    用过windows7的朋友都知道,Windows 7 中设置某一个文件夹属性为共享后,文件夹的图标上就增加一个小锁图案.起到了一个标记作用,挺好的.但是即使你将该文件夹的共享功能取消后,该小锁图案还是 ...

  7. js 对url字符转译全解

    1.js 对url进行字符解码设计到3个方法 escape , encodeURI , encodeURIComponent eg: var url='http://baidu.com';encode ...

  8. NSString、NSData、char* 类型之间的转换-备

    1. NSString转化为UNICODE String: (NSString*)fname = @“Test”; char fnameStr[10]; memcpy(fnameStr, [fname ...

  9. Here are some of my ideas .

    1:Learning english is very important ,its the very useful for my major studying and my future develo ...

  10. 处理emacs-org模式TODO的一个脚本

    处理前: 处理后: Table of Contents 1 前言 2 中文的处理 2.1 vim相关 2.2 perl 相关 3 时间相关 4 程序解析 1 前言 最近风帆问我一个问题,也就是处理or ...