码迷,mamicode.com
首页 > 编程语言 > 详细

cocos2d-x 2.2.6中c++通过JNI与java互调

时间:2015-06-26 17:53:31      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

1.HelloCpp.java

 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 org.cocos2dx.hellocpp;
25 
26 import org.cocos2dx.lib.Cocos2dxActivity;
27 
28 
29 import android.app.AlertDialog;
30 import android.content.DialogInterface;
31 import android.os.Bundle;
32 import android.os.Handler;
33 import android.os.Message;
34 
35 public class HelloCpp extends Cocos2dxActivity{
36     static 
37     {
38          System.loadLibrary("hellocpp");
39     }
40     //java调用c++
41     public static native void javaToC();
42     
43     
44     
45     private static AlertDialog mDialog = null;
46     
47     
48     protected void onCreate(Bundle savedInstanceState){
49         super.onCreate(savedInstanceState);
50          mDialog = new AlertDialog.Builder(this).create();
51             mDialog.setButton("确定", new AlertDialog.OnClickListener() {
52                 @Override
53                 public void onClick(DialogInterface dialog, int which) {
54                     HelloCpp.this.finish();
55                 }
56             });
57             
58             mDialog.setButton2("取消", new AlertDialog.OnClickListener() {
59                 @Override 
60                 public void onClick(DialogInterface dialog, int which) {
61                 }
62             });
63             mDialog.setButton3("HelloC", new AlertDialog.OnClickListener()
64             {
65                 @Override public void onClick(DialogInterface dialog, int which) {
66                      // Java调用c代码
67                      HelloCpp.this.javaToC();
68                 }
69             });
70            
71     }
72     
73 
74    //c++会传入string[]然后调用c++此方法 此方法再调用下面handler方法
75     public static void sayHelloCToJava(String[] str){
76         Message mes = new Message();
77         mes.obj = str;
78         mHandler.sendMessage(mes);
79     }
80     
81     private static Handler mHandler = new Handler(new Handler.Callback() 
82     {
83         @Override
84         public boolean handleMessage(Message msg) {
85         String[] str = (String[])msg.obj;
86         mDialog.setTitle(str[0]);
87         mDialog.setMessage(str[1]);
88         mDialog.show();
89         return true;
90         }
91     });
92 }

 

 

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(2 , str_cls , 0);
           info.env->SetObjectArrayElement(arrs , 0 , str1);
           info.env->SetObjectArrayElement(arrs , 1 , 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互调

标签:

原文地址:http://www.cnblogs.com/sweetculiji/p/4602806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!