,

Call Java functions in Cocos2d-x

Java [code lang=”java”] public static Activity _activiy; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Save activity instance _activiy = this; } public static void alertJNI() { String tag = “JniTest”; String message = “I’ve been called from C++”; Log.d(tag, “Showing alert dialog: ” + message); } [/code] C++ First we need to include JniHelper and…


Java

[code lang=”java”]
public static Activity _activiy;

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

//Save activity instance
_activiy = this;
}

public static void alertJNI() {
String tag = “JniTest”;
String message = “I’ve been called from C++”;
Log.d(tag, “Showing alert dialog: ” + message);
}
[/code]

C++

First we need to include JniHelper and jni headers

[code lang=”c”]
#include “platform/android/jni/JniHelper.h”
#include <jni.h>
[/code]

Call java function from C++ code

[code lang=”c”]
void HelloWorld::menuCloseCallback(Ref* pSender)
{
cocos2d::JniMethodInfo methodInfo;

if (! cocos2d::JniHelper::getStaticMethodInfo(methodInfo, “org/cocos2dx/cpp/AppActivity”, “alertJNI”, “()V”)) {
return;
}

methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
[/code]


Leave a Reply

Discover more from Prabhu System Technology

Subscribe now to keep reading and get access to the full archive.

Continue reading