首页 > 代码库 > Cocos2d-x 游戏中嵌入广告(百度插屏)

Cocos2d-x 游戏中嵌入广告(百度插屏)

1、声明Activity

 <!-- 声明百度的Activity -->
		<meta-data android:name="BaiduMobAd_APP_ID" android:value=http://www.mamicode.com/"debug" /> >


 

2、在主Activity类中,初始广告对象

interAd=new InterstitialAd(this);
		interAd.setListener(new InterstitialAdListener(){

			@Override
			public void onAdClick(InterstitialAd arg0) {
				Log.i("InterstitialAd","onAdClick");
			}

			@Override
			public void onAdDismissed() {
				Log.i("InterstitialAd","onAdDismissed");
				interAd.loadAd();
			}

			@Override
			public void onAdFailed(String arg0) {
				Log.i("InterstitialAd","onAdFailed");
			}

			@Override
			public void onAdPresent() {
				Log.i("InterstitialAd","onAdPresent");
			}

			@Override
			public void onAdReady() {
				Log.i("InterstitialAd","onAdReady");
			}
			
		});
		//加载广告
		interAd.loadAd();

3、显示广告

handler = new Handler(){
		
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch (msg.what) {
				case 3:
							
						//public void onClick( View arg0 ) {
							if(interAd.isAdReady()){//读取是否成功
								Log.i("InterstitialAd","showAd");
								interAd.showAd(Test.this);//显示
							}else{
								interAd.loadAd();
								Log.i("InterstitialAd","loadAd...");
							}

					break;
				}
			}
		};


4、定义静态函数,用于cpp调用

public static void showInterstitialStatic() {
		Message msg = handler.obtainMessage();
		msg.what = 3;
		handler.sendMessage(msg);
	}


5、效果图,点show