Cocos2d-x 游戏中嵌入广告(百度横幅)

转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/36716955

DEMO下载地址:http://download.csdn.net/detail/oyangyufu/7589981

上一篇写了cocos2d-x嵌入mogo横幅广告(点击此跳转),这篇写如何在cocos2d-x中嵌入百度横幅广告。

百度横幅广告测试:

1、将cocos2d-x项目导入到eclipse上

2、注册登入百度移动联盟(http://munion.baidu.com/)后,下载推广SDK android 版,下载完名称为:Baidu_MobAds_SDK.zip

3、解压后获取jar包,Baidu_MobAds_SDK.jar,放入libs目录下,单击右键->Build Path->Add to Build Path

4、在AndroidManifest.xml添加用户权限:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>

5、添加APPSID与计费名及申明AppActivity

   <!-- 声明百度的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" />
		<!--baidu广告 end  -->

注此时测试APPSID和计费名的值为debug,调试成功后改为自己申请成功的APPSID和计费名,即可上传应用至联盟端审核,审核通过后便显示广告并计费。

6、在主Activity类中,添加横幅广告

package com.cocos2dx.org;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

import org.json.JSONObject;

import com.baidu.mobads.AdSettings;
import com.baidu.mobads.AdView;
import com.baidu.mobads.AdViewListener;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

public class Test extends Cocos2dxActivity{
	public static SharedPreferences sharedPreferences;
	public static SharedPreferences.Editor editor;
	
	private static Handler handler;
	private static RelativeLayout bannerLayout;

	private AdView adView;
	
    protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);	
		
		bannerLayout = new RelativeLayout(this);
		RelativeLayout.LayoutParams parentLayputParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		
		this.addContentView(bannerLayout, parentLayputParams);
		
		
		adView = new AdView(this);
		handler = new Handler(){
		
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch (msg.what) {
				case 0:
					
					if (bannerLayout.getChildCount() == 0) {
					// 设置监听器						
						adView.setListener(new AdViewListener() {
						public void onAdSwitch() {
							Log.w("", "onAdSwitch");
						}
						public void onAdShow(JSONObject info) {
							Log.w("", "onAdShow " + info.toString());
						}
						public void onAdReady(AdView adView) {
							Log.w("", "onAdReady " + adView);
						}
						public void onAdFailed(String reason) {
							Log.w("", "onAdFailed " + reason);
						}
						public void onAdClick(JSONObject info) {
							Log.w("", "onAdClick " + info.toString());
						}
						public void onVideoStart() {
							Log.w("", "onVideoStart");
						}
						public void onVideoFinish() {
							Log.w("", "onVideoFinish");
						}
						@Override
						public void onVideoClickAd() {
							Log.w("", "onVideoFinish");
						}
						@Override
						public void onVideoClickClose() {
							Log.w("", "onVideoFinish");
						}
						@Override
						public void onVideoClickReplay() {
							Log.w("", "onVideoFinish");
						}
						@Override
						public void onVideoError() {
							Log.w("", "onVideoFinish");
						}
					});
						//显示广告
						RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
								RelativeLayout.LayoutParams.MATCH_PARENT ,
								RelativeLayout.LayoutParams.WRAP_CONTENT);
						layoutParams.addRule(
								RelativeLayout.ALIGN_PARENT_BOTTOM,
								RelativeLayout.TRUE);
						bannerLayout.addView(adView, layoutParams);
					}
					break;
				}
			}
		};
			   
	}

    public Cocos2dxGLSurfaceView onCreateView() {
    	Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
    	// HelloWorld should create stencil buffer
    	glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
    	
    	return glSurfaceView;
    }
    //静态函数cpp调用
		public static void showBannerStatic() {
			Message msg = handler.obtainMessage();
			msg.what = 0;
			handler.sendMessage(msg);
		}
		

    static {
        System.loadLibrary("cocos2dcpp");
    }     
}


 

 

7、在CPP程序中通过JNI调用showBannerStatic()函数即可。

8、效果图如下,点击show banner按钮显示广告

Cocos2d-x 游戏中嵌入广告(百度横幅)_第1张图片

 

你可能感兴趣的:(Cocos2d-x 游戏中嵌入广告(百度横幅))