Passing an integer with JNI

Passing an integer with JNI
0

#1

Does anyone know how I can pass an integer (the score from my game) to AppAcitivty.java in order to show this on a pop menu that allows user to share it ?
so far I have this but im not sure where to go from here ?

void AdHelper::shareMyScore(int score)
{
cocos2d::JniHelper::callStaticVoidMethod(“org.cocos2dx.cpp/AppActivity”, “shareMyScore”);
}


#2

Now you need to implement the method in AppActivity:

public static void shareMyScore() {
    final AppActivity activity = ((AppActivity)Cocos2dxHelper.getActivity());

    activity.runOnGLThread(new Runnable() {
        @Override
        public void run() {
            // your code
        }
    });
}

Insert your code in run method, it will be executed in GL thread.


#3

this is what I have in my AppActivity, how do I get the integer parameter (score) from .cpp to .java ?

public static void shareMyScore(int score)
{

}


#4

Sorry, forgot about integer :slight_smile:

C++

void AdHelper::shareMyScore(int score)
{
    cocos2d::JniHelper::callStaticVoidMethod(“org.cocos2dx.cpp.AppActivity”, “shareMyScore”, score);
}

Java

public static void shareMyScore(final int score) {
    final AppActivity activity = ((AppActivity)Cocos2dxHelper.getActivity());

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // your code
        }
    });
}

#5

I was looking at some other code , is
final AppActivity activity = ((AppActivity)Cocos2dxHelper.getActivity()); does the same thing as
private static Activity activity = AppAcitivity.getInstance(); ?

also in this example I was following they called method.env->DeleteLocalRef(stringMessage); do I have to do something similar for the integer im passing in ?


#6

yes AppAcitivity.getInstance() the same

no, for int no need.


#7

ok thanks verymuch for your help!

also is
“acitvity.runOnGlThread”

the same thing as “activity.runOnUiThread” ??


#8

Sorry, for your case runOnUiThread is needed.

  • runOnGlThread - use this to call C++ code from Java, so C++ code will be executed in cocos2d thread (GL)
  • runOnUiThread - use this to call Java code from C++, so Java code will be executed in Java’s UI thread

#9

ok thanks once again :smiley::+1:


#10

Glad to help :wink: