facebook twitter hatena line email

「Cocos2dx/AndroidNative連携/コールバック」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行16: 行16:
 
     JNIEXPORT void JNICALL Java_com_example_helloworld_SampleBridge_cocosOnCustomEvent(JNIEnv* env, jobject thiz, jstring srcj) {
 
     JNIEXPORT void JNICALL Java_com_example_helloworld_SampleBridge_cocosOnCustomEvent(JNIEnv* env, jobject thiz, jstring srcj) {
 
         const char *message = env->GetStringUTFChars(srcj, NULL);
 
         const char *message = env->GetStringUTFChars(srcj, NULL);
         log("success com_example_helloworld_SampleBridge message=%s", message);
+
         log("success com_example_helloworld_SampleBridge message=%s", message); // launchCallbackNativeMessage
 
     }
 
     }
 
  }
 
  }

2017年3月2日 (木) 14:16時点における版

サンプルコード

-Classes/com_example_helloworld_SampleBridge.h

#include <jni.h>
#ifndef _Included_com_example_helloworld_SampleBridge
#define _Included_com_example_helloworld_SampleBridge
extern "C" {
    JNIEXPORT void JNICALL Java_com_example_helloworld_SampleBridge_cocosOnCustomEvent(JNIEnv*, jobject, jstring);
}
#endif // com_example_helloworld_SampleBridge

-Classes/com_example_helloworld_SampleBridge.cpp

#include "cocos2d.h"
#include "com_example_helloworld_SampleBridge.h"
USING_NS_CC;
extern "C" {
    JNIEXPORT void JNICALL Java_com_example_helloworld_SampleBridge_cocosOnCustomEvent(JNIEnv* env, jobject thiz, jstring srcj) {
        const char *message = env->GetStringUTFChars(srcj, NULL);
        log("success com_example_helloworld_SampleBridge message=%s", message); // launchCallbackNativeMessage
    }
}

-Classes/NativeLauncher.h

#ifndef Helloworld_NativeLauncher_h
#define Helloworld_NativeLauncher_h
extern "C" {
    class NativeLauncher
    {
    public:
        static void launchCallbackNative(); // コールバック呼び出しメソッド
    };
}
#endif

-Classes/NativeLauncher.cpp

#include "NativeLauncher.h"
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#define CLASS_NAME "org/cocos2dx/cpp/AppActivity"
extern "C" {
   void NativeLauncher::launchCallbackNative()
   {
       cocos2d::JniMethodInfo t;
       if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "launchCallbackNative", "()V")) {
           t.env->CallStaticVoidMethod(t.classID, t.methodID);
           t.env->DeleteLocalRef(t.classID);
       }
   }
}

-Classes/HelloWorldScene.h

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    virtual bool init();
    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);
    void menuLogCallback(cocos2d::Ref* pSender);
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

-Classes/HelloWorldScene.cpp

#include "NativeLauncher.h"
bool HelloWorld::init()
{
// 略
   auto logItem = MenuItemImage::create(
                                          "CloseNormal.png",
                                          "CloseSelected.png",
                                          CC_CALLBACK_1(HelloWorld::menuLogCallback, this));
   logItem->setPosition(Vec2(origin.x + visibleSize.width - logItem->getContentSize().width/2 ,
                                origin.y + visibleSize.height - logItem->getContentSize().height/2));
   auto logmenu = Menu::create(logItem, NULL);
   logmenu->setPosition(Vec2::ZERO);
   this->addChild(logmenu, 1);
// 略
}
void HelloWorld::menuLogCallback(Ref* pSender)
{
    NativeLauncher::launchCallbackNative();
}

-proj.android-studio/app/src/org/cocos2dx/cpp/AppActivity.java

import com.example.helloworld.SampleBridge;
public class AppActivity extends Cocos2dxActivity {
    public static void launchCallbackNative() {
        System.out.println("launchCallbackNative()");
        (new SampleBridge()).onCustomEvent("launchCallbackNativeMessage");
    }
}

-proj.android/jni/Android.mk

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp \
                   ../../Classes/NativeLauncher.cpp \
                   ../../Classes/com_example_helloworld_SampleBridge

NativeLauncher.cppとcom_example_helloworld_SampleBridgeを追加

-proj.android-studio/jni/Android.mk

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp \
                   ../../Classes/NativeLauncher.cpp \
                   ../../Classes/com_example_helloworld_SampleBridge

NativeLauncher.cppとcom_example_helloworld_SampleBridgeを追加

-proj.android-studio/app/src/com/example/helloworld/SampleBridge.java

package com.example.helloworld;
/**
 * androidでは(new SampleBridge).onCustomEvent();で呼び出し可能
 */
public class SampleBridge {
    private static native void cocosOnCustomEvent(String message);
    public void onCustomEvent(final String message) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("SampleBridge.onCustomEvent");
                SampleBridge.cocosOnCustomEvent(message);
            }
        });
        thread.start();
    }
}

参考

https://techblog.ca-reward.co.jp/2017/02/post-140.html