show & hide Admob ad from c++ class

I managed to integrate Admob correctly with my cocos2dx project. The ad displays fine on initialization from java code.

I then made 2 functions in java showAd() and hideAd() which basically just calls adView.SetVisibility() function to show/hide the Admob ad.

Now the problem begins…

I call showAd from jni, the call throws an exception (just says null) and crashes my app (I’m calling the jni function properly). But when I call the same function from java, it executes properly, thus showing the ad. I’m not sure where the problem is? Please help.

Below is my Initialize call for setting up Admob

public void initAdmobAds() { try{ LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams( 1024, 768-110); adView = new AdView(abcapp.this, AdSize.BANNER, "a15125246095824"); adView.loadAd(new AdRequest()); // Adding full screen container addContentView(adView, adParams); adView.setVisibility(AdView.INVISIBLE); }catch (Exception e) { System.out.print("ADMOB------>ADMOB error (init): "); System.out.println(e.getMessage()); } }

Now the 2 functions to show & hide the Admob adView

`public void showadspopup()
{
adView.setVisibility(AdView.VISIBLE);
}

public void hideadspopup()
{
    adView.setVisibility(AdView.INVISIBLE);
}`

And finally the C++ call from code calling the showadspopup function to display the ad

void HelloWorld::showAdsPopupScreen() { jint status; jmethodID mid; jclass mClass; JavaVM* jvm = JniHelper::getJavaVM(); JNIEnv* env; status = jvm->GetEnv((void**)&env,JNI_VERSION_1_4); jclass clazz = env->FindClass("com/joyhouse/abcapp/abcapp"); jmethodID method = env->GetMethodID(clazz, "showadspopup", "()V"); env->CallStaticVoidMethod(mClass,method); return; }

ok, found the problem. I was calling the showAd() function on another thread instead of the main thread. When you call from JNI, you need to ensure you get the context of your activity and then make sure you are calling your function on your main thread.

So I’ve modified my code below to make sure I run on my main thread. So it works!

public void showadspopup() { try{ ((Activity)mContext).runOnUiThread(new Runnable() {Override
public void run() {
adView.setVisibility(AdView.VISIBLE);
}
});

}catch(Exception e){
System.out.println(e.getMessage());
}

}@

Hi,
I use static methods and a handler in Java to do this, something like:

In you activity class derived from Cocos2dxActivity have a Handler and

    public static GameActivity me = null;
    private static Handler handler;

in your onCreate method

GameActivity.me = this;
handler = new Handler(){
     public void handleMessage(Message msg)
     {
          switch (msg.what)
          {
               case HANDLER_SHOW_AD:
                  setShowAd(true);
                  break;
               case HANDLER_HIDE_AD:
                  setShowAd(false);
                  break;
          }
     }
};

then

public void setShowAd(boolean visible)
{
     AdView adView = (AdView) this.findViewById(R.id.adView);
     if (visible)
     {
          adView.setVisibility(AdView.VISIBLE);
          adView.bringToFront();
     }
     else
     {
          adView.setVisibility(AdView.INVISIBLE);
     }
}

public static void showAd(boolean visible)
{
     Message msg = new Message();
     if (visible)
         msg.what = HANDLER_SHOW_AD;
     else
         msg.what = HANDLER_HIDE_AD;

     handler.sendMessage(msg);
}

and in c++

void showAdJNI(bool show)
{
    JniMethodInfo methodInfo;

    if (! getStaticMethodInfo(methodInfo, "showAd", "(Z)V"))
    {           
        return;
    }

    bool s=show;
    methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, s);
    methodInfo.env->DeleteLocalRef(methodInfo.classID);
}

This should make it work

Hope this helps

Hi Srini, can you give me a detail, or even your complete onCreate method? and the static java method that called by cocos?
I see you call static method from cocos “env->Call*Static*VoidMethod(mClass,method);”
But in Java, you declare your the method as “public void showadspopup” -without static
It will cause error on me because the cocos not found the static function.
And, I can’t get the adView reference from static method.
Does anyone know how to solve it?
Thanks
@christian : I’ve been trying your code. But i don’t understand what theGameActivity me is. I’ve try to use my main class that extends the CocosActivity and implements AdListener right? And then you assign it, and doing nothing with it to the rest of your code. Mine is compiled without error, but then it will cause force close.