4

I'm trying to add in-app billing in my cocos2dx-android project. I am able to give a call to java function from c++ class via jni.

This is the way i give a call to my java function via jni.

    JniMethodInfo t;
        JniHelper::getStaticMethodInfo(t
                , "com/test/project/Project" 
                , "BuyProduct" 
                , "(Ljava/lang/String;)V");

        char buffer[20];
        sprintf(buffer,"product1");
        jstring StringArg1 = t.env->NewStringUTF(buffer);

    t.env->CallStaticVoidMethod(t.classID, t.methodID, StringArg1);

The in-app billing is working fine, but now i have to give a call back to my c++ class to inform if the product purchase was successful or not.

I could return the result from the called method only by mentioning the specified return type, but the in-app process being a asynchronous process-goes through a lot of method calls, and my control is not returned back to the same method. So returning a value would not work.

So is there any other way of passing a value(in my case the result of in-app purchase) to my c++ class from java function???

Komal
  • 43
  • 1
  • 7

1 Answers1

3

check the cocos2dxHelper.cpp file to have a look how cocos2dx call c++ method. basically there is a method in cocos2dxHelper.java which only have the definition but not the implementation, usually it looks like

public static native blahblah();

and there is a corresponding function in cpp file called

Java_org_cocos2dx_cocos2dxHelper_blahblah()

if you call the blahblah() in Java code with runOnUIThread(), the c++ function

Java_org_cocos2dx_cocos2dxHelper_blahblah()

will be called.

by the way, the c++ code need to be in something extern C { }

m.ding
  • 3,172
  • 19
  • 27
  • i tried what u it and it worked. I created a new cpp class with name Jave__ and gave a call to my cpp method from java. – Komal Jun 01 '13 at 07:40
  • now i'm facing a new problem, after the control comes back to cpp code all of my text(CCLabelTTF's text) disappers and some of the assets are not rendered and instead shows me black blocks. What to do in this regard?? – Komal Jun 01 '13 at 07:43
  • @Komal Sorry that problem never happen to me before, can't help much. – m.ding Jun 02 '13 at 22:42
  • @Komal I guess it might beecause coos2d-x is not thread safe and you want to update the CCLableTTF not on the main thread. instead of update the lable in the JNI calls, try to store the value locally first in your class and update the CCLabelTTF in a schedual_selector function (or Update(float t)) – m.ding Jun 06 '13 at 03:48
  • i am actually using a CCTableView for displaying my products for purchase. CCTableView dynamically added the items(CCLabelTTF's, CCSprites). So once the call comes back to cpp code. It renders the CCSprites(most of them) but there is issue only with CCLabelTTF's. Once i scroll the table the text appears(as it is dynamically added(rendered)) – Komal Jun 07 '13 at 12:54
  • @Komal Then it might be a bug in CCTableView. – m.ding Jun 08 '13 at 00:01