前端之家收集整理的这篇文章主要介绍了
在cocos2d-x中如何嵌入百度的插屏广告,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
DEMO下载:http://download.csdn.net/detail/oyangyufu/7652803
演示的效果请看这里:http://pan.baidu.com/s/1o6oit9w
1、声明Activity
- <!--声明百度的Activity-->
- <Meta-dataandroid:name="BaiduMobAd_APP_ID"android:value="debug"/>
- <Meta-dataandroid:name="BaiduMobAd_APP_SEC"android:value="debug"/>
- <activityandroid:name="com.baidu.mobads.AppActivity"
- android:configChanges="keyboard|keyboardHidden|orientation"/>
<!-- 声明百度的Activity -->
<Meta-data android:name="BaiduMobAd_APP_ID" android:value="debug" />
<Meta-data android:name="BaiduMobAd_APP_SEC" android:value="debug" />
<activity android:name="com.baidu.mobads.AppActivity"
android:configChanges="keyboard|keyboardHidden|orientation" />
2、在主Activity类中,初始广告对象
- interAd=newInterstitialAd(this);
- interAd.setListener(newInterstitialAdListener(){
- @Override
- publicvoidonAdClick(InterstitialAdarg0){
- Log.i("InterstitialAd","onAdClick");
- }
- @Override
- publicvoidonAdDismissed(){
- Log.i("InterstitialAd","onAdDismissed");
- interAd.loadAd();
- }
- @Override
- publicvoidonAdFailed(Stringarg0){
- Log.i("InterstitialAd","onAdFailed");
- }
- @Override
- publicvoidonAdPresent(){
- Log.i("InterstitialAd","onAdPresent");
- }
- @Override
- publicvoidonAdReady(){
- Log.i("InterstitialAd","onAdReady");
- }
- });
- interAd.loadAd();
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=newHandler(){
- @Override
- publicvoidhandleMessage(Messagemsg){
- switch(msg.what){
- case3:
- if(interAd.isAdReady()){
- Log.i("InterstitialAd","showAd");
- interAd.showAd(Test.this);
- }else{
- interAd.loadAd();
- Log.i("InterstitialAd","loadAd...");
- }
- break;
- }
- }
- };
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调用
- publicstaticvoidshowInterstitialStatic(){
- Messagemsg=handler.obtainMessage();
- msg.what=3;
- handler.sendMessage(msg);
- }
public static void showInterstitialStatic() {
Message msg = handler.obtainMessage();
msg.what = 3;
handler.sendMessage(msg);
}
5、效果图,点show