title: Android Cocos2dX JNI Bridge
body: <p>I am new to using Cocos2d-X and am just experimenting using JNI in my application. So here is my java code</p>
  
  <pre><code>public class JNITest {
      public static native void printSomething();
  
      public static void printSomethingFromJava(){    
          printSomething();
      }
  
  }
  </code></pre>
  
  <p>I use javah to generate a header file, and implement a C function in my MyScene.cpp file</p>
  
  <pre><code>void notify(){
       CCNotificationCenter::sharedNotificationCenter()-&gt;postNotification("hello",NULL);
  }
  
  extern "C" {
      void Java_com_nbs_test_JNITest_printSomething(JNIEnv *, jclass){
          CCLog("THE jni call is successfull");
          notify();
      }
  }
  </code></pre>
  
  <p>The CCLog message is printed, so my android -> c++ bridge is working. In the constructor of 
  MyScene.cpp i set up a Listener </p>
  
  <pre><code>MyScene::MyScene() {
  
      if (!CCLayer::init()) {
          return;
      }
       CCNotificationCenter::sharedNotificationCenter()-&gt;addObserver(
                        this,
                        callfuncO_selector( MyScene::printSomethingInCpp),
                        "hello",
                        NULL);
  </code></pre>
  
  <p>and in the  MyScene::printSomethingInCpp i just print this</p>
  
  <pre><code>void MyScene::printSomethingInCpp(){
      CCLog("Its goton hererew----------------------------&gt;");
  }
  </code></pre>
  
  <p>The log message in PingoScreen::printSomethingInCpp is never printed. I dont know if the problem is with my JNI call or with the Observer pattern ? </p>
  
  <h1> comeplete code</h1>
  
  <pre><code>#ifndef PINGOSCREEN_H_
  #define PINGOSCREEN_H_
  #include "RequestHandler.h"
  #include "cocos2d.h"
  #include "cocos-ext.h"
  #include "box2d.h"
  #include "json.h"
  #include "GLES-Render.h"
  
  #define MY_SCREEN_RES "hello"
  class PingoScreen: public cocos2d::CCLayer{
  public:
      bool init();
          PingoScreen();
          ~PingoScreen();
          static cocos2d::CCScene* scene();
          CREATE_FUNC(PingoScreen);
          void printSomethingInCpp(CCObject *pObject);
          static void notify();
          static PingoScreen* getInstance();
  
          void setListener();
  private:
          cocos2d::CCSprite *ball;
          RequestHandler* r;
  
  };
  </code></pre>
  
  <p>AND</p>
  
  <pre><code>#include "PingoScreen.h"
  #include "SampleRequest.h"
  #include "platform/android/jni/JniHelper.h"
  
  
  #define PTM_RATIO 32;
  using namespace cocos2d;
  static PingoScreen* _mInstance = NULL;
  
  CCScene* PingoScreen::scene() {
      CCScene* pScene = CCScene::create();
      PingoScreen* pingoScreen =PingoScreen::create();
      pScene-&gt;addChild(pingoScreen);
      return pScene;
  }
  
  
  
  PingoScreen* PingoScreen::getInstance(){
      if(!_mInstance){
          _mInstance=PingoScreen::create();
      }
      return _mInstance;
  }
  
  
  
  
  void PingoScreen::setListener(){
  }
  
  
  bool PingoScreen::init(){
      if (!CCLayer::init()) {
              return false;
      }
  
          // CCNotificationCenter::sharedNotificationCenter()-&gt;postNotification("hello",NULL);
       CCNotificationCenter::sharedNotificationCenter()-&gt;addObserver(
                   this,
                   callfuncO_selector(PingoScreen::printSomethingInCpp),
                   "hello",
                   NULL);
  
          CCSize pSize = CCDirector::sharedDirector()-&gt;getWinSize();
          CCSprite* backgroundSprite = CCSprite::create("room5.png");
          backgroundSprite-&gt;setAnchorPoint(CCPointZero);
          this-&gt;addChild(backgroundSprite, -1);
          return true;
  }
  PingoScreen::PingoScreen() {
  
  
  }
  
  PingoScreen::~PingoScreen() {
  
  }
  
  
  void PingoScreen::printSomethingInCpp(CCObject *pObject){
      CCLog("Its goton hererew 1212112----------@@@@@@@@@@@@@@@212121212@@@@@@@@@@@@@@@------------------&gt;");
  }
  
  void PingoScreen::notify(){
      CCNotificationCenter::sharedNotificationCenter()-&gt;postNotification("hello",NULL);
  }
  
  extern "C" {
      void Java_com_nbs_test_JNITest_printSomething(JNIEnv *, jclass){
          PingoScreen::notify();
      }
  }
  </code></pre>