在cocos2dx中加入Admob插页广告,不能用JNI直接调用
if (interstitial.isLoaded()) { interstitial.show(); }
显示插页广告的函数必须在mainUIThread中调用。
否则会报错:
IllegalStateException: XXXX must be called on the main UI thread.
所以要通过android handler传递消息来调用广告:
private static Handler handler; private final static int HANDLER_GOTO_ADMOB = 1;______________________________________________ handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case HANDLER_GOTO_ADMOB: // 显示广告 if (interstitial.isLoaded()) { interstitial.show(); } else { AdRequest adRequest = new AdRequest.Builder().build(); interstitial.loadAd(adRequest); } break; case HANDLER_GOTO_OTHER: // 隐藏广告 break; } } };________________________________________________ public static void displayInterstitial() { Message msg = new Message(); msg.what = HANDLER_GOTO_ADMOB; handler.sendMessage(msg); }
参考: