用cocos2d-x 3.4 开发了个小游戏,然后成功的接入百度联盟的广告,很多初学者也希望在自己开发的游戏接入广告盈利,所以我就以百度联盟的横幅和插屏广告为例,根据自己接入的步骤做个总结,希望对有需要的开发者有所帮助。
一、百度SDK包导入与权限的配置
1、导入jar包
下载百度联盟androID版的SDK包,我下载的文件名BaIDu_Union_SDK_AndroID.zip,解压后文件夹里面有个jar包BaIDu_MobAds_SDK.jar,将该jar包复制到你工程的libs目录下(如果没有新建一个libs文件夹),然后右键点击你的项目->PropertIEs->Java Build Path->librarIEs->Add JARs添加百度jar包,如下图所示:
650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="QQ拼音截图未命名.png" alt="wKiom1YBTTbhWC-3AAHXkG1q-Yo732.jpg" src="http://s3.51cto.com/wyfs02/M00/73/95/wKiom1YBTTbhWC-3AAHXkG1q-Yo732.jpg">
2、配置AndroIDManifest.xml
◆添加权限
<uses-permissionandroID:name="androID.permission.INTERNET"/><uses-permissionandroID:name="androID.permission.READ_PHONE_STATE"/><uses-permissionandroID:name="androID.permission.ACCESS_NETWORK_STATE"/><uses-permissionandroID:name="androID.permission.ACCESS_COARSE_LOCATION"/><uses-permissionandroID:name="androID.permission.WRITE_EXTERNAL_STORAGE"/><uses-permissionandroID:name="androID.permission.ACCESS_WIFI_STATE"/><!--以下为可选权限--><uses-permissionandroID:name="androID.permission.CHANGE_WIFI_STATE"/><uses-permissionandroID:name="androID.permission.RECORD_AUdio"/><uses-permissionandroID:name="androID.permission.VIBRATE"/><uses-permissionandroID:name="androID.permission.CAMERA"/><uses-permissionandroID:name="androID.permission.ACCESS_FINE_LOCATION"/><uses-permissionandroID:name="com.androID.browser.permission.READ_HISTORY_BOOKMARKS"/>
◆添加APPSID和AppActivity申明
<Meta-dataandroID:name="BaIDuMobAd_APP_ID"androID:value="dcd69c04"/><Meta-dataandroID:name="BaIDuMobAd_APP_SEC"androID:value="dcd69c04"/><activityandroID:name="com.baIDu.mobads.AppActivity"androID:configChanges="keyboard|keyboardHIDden|orIEntation"/>
如下图所示:
650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="QQ拼音截图未命名.png" alt="wKioL1YBT1aBrdfRAAJ9S5RSVew859.jpg" src="http://s3.51cto.com/wyfs02/M00/73/92/wKioL1YBT1aBrdfRAAJ9S5RSVew859.jpg">
二、横幅banner广告的接入
在AppActivity.java文件中加入如下代码
privateAdVIEwadVIEw;@OverrIDeprotectedvoIDonCreate(BundlesavedInstanceState){//Todoauto-generatedmethodstubsuper.onCreate(savedInstanceState);setupAds();}privatevoIDsetupAds(){relativeLayoutlayout=newrelativeLayout(this);addContentVIEw(layout,newLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));adVIEw=newAdVIEw(this);relativeLayout.LayoutParamslayoutParams=newrelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);layoutParams.addRule(relativeLayout.AliGN_PARENT_BottOM);layout.addVIEw(adVIEw,layoutParams);}
如图所示:
650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="QQ拼音截图未命名.png" alt="wKiom1YBUt6xjJF7AAOaxjQDuDk796.jpg" src="http://s3.51cto.com/wyfs02/M01/73/95/wKiom1YBUt6xjJF7AAOaxjQDuDk796.jpg">
加入代码后广告会在游戏的底端显示三、加入插屏广告
1、在类AppActivity同一包名下新建ShowAdHelper类文件
650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="QQ拼音截图未命名.png" alt="wKioL1YBVLHygqp5AABKlRL_2Cs059.jpg" src="http://s3.51cto.com/wyfs02/M01/73/92/wKioL1YBVLHygqp5AABKlRL_2Cs059.jpg">
2、在类ShowAdHelper中加入如下代码
publicclassShowAdHelper{privatestaticHandlermHandler;publicstaticvoIDinit(Handlerhandler){ShowAdHelper.mHandler=handler;}privatestaticvoIDshowMyAd(){Messagemsg=mHandler.obtainMessage();msg.what=AppActivity.SHOW_AD;msg.sendToTarget();}}
3、在类ShowAdHelper中定义一个变量和一个常量
publicstaticfinalintSHOW_AD=0x0001;InterstitialAdinterad;
3、在类AppActivity中新建getInsertAd()方法初始化插屏广告
publicInterstitialAdgetInsertAd(){finalinterstitialAdinterad1=newInterstitialAd(this);interad1.setListener(newInterstitialAdListener(){@OverrIDepublicvoIDonAdClick(InterstitialAdarg0){Log.i("InterstitialAd","onAdClick");}@OverrIDepublicvoIDonAddismissed(){Log.i("InterstitialAd","onAddismissed");interad1.loadAd();}@OverrIDepublicvoIDonAdFailed(Stringarg0){Log.i("InterstitialAd","onAdFailed");}@OverrIDepublicvoIDonAdPresent(){Log.i("InterstitialAd","onAdPresent");}@OverrIDepublicvoIDonAdReady(){Log.i("InterstitialAd","onAdReady");}});interad1.loadAd();returninterad1;}
4、创建Handler
privateHandleraHandler=newHandler(){@OverrIDepublicvoIDhandleMessage(Messagemsg){switch(msg.what){caseSHOW_AD:if(interad.isAdReady()){interad.showAd(AppActivity.this);Log.d("LC","show");}else{interad.loadAd();}Log.d("LC","notshow");break;}}};
5、在onCreate加入代码
@OverrIDeprotectedvoIDonCreate(BundlesavedInstanceState){//Todoauto-generatedmethodstubsuper.onCreate(savedInstanceState);interad=getInsertAd();ShowAdHelper.init(aHandler);}
6、java层的编写就此结束了,接下去在C++层中通过jni调用ShowAdHelper类中的showMyAd函数
voIDBackgroundLayer::loadAndroIDAd(){#if(CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)//判断当前是否为AndroID平台JniMethodInfominfo;jobjectjobj;boolb=JniHelper::getStaticmethodInfo(minfo,"org/cocos2dx/cpp/ShowAdHelper","showMyAd","()V");jobj=minfo.env->CallStaticObjectMethod(minfo.classID,minfo.methodID);#endif}
同时在BackgroundLayer文件开头加入
#if(CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)#include<jni.h>#include"platform/androID/jni/JniHelper.h"#endif
然后在所需显示插屏广告的地方调用loadAndroIDAd()函数即可
总结以上是内存溢出为你收集整理的cocos2d-x学习笔记(五)接入百度联盟广告(Android横幅和插屏广告)全部内容,希望文章能够帮你解决cocos2d-x学习笔记(五)接入百度联盟广告(Android横幅和插屏广告)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)