cocos2d-x学习笔记(五)接入百度联盟广告(Android横幅和插屏广告)

前端之家收集整理的这篇文章主要介绍了cocos2d-x学习笔记(五)接入百度联盟广告(Android横幅和插屏广告)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

用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包,如下图所示:

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"/>

如下图所示:

wKioL1YBT1aBrdfRAAJ9S5RSVew859.jpg


二、横幅banner广告的接入

在AppActivity.java文件中加入如下代码

privateAdViewadView;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
//TODOAuto-generatedmethodstub
super.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);
}

如图所示:

wKiom1YBUt6xjJF7AAOaxjQDuDk796.jpg

加入代码后广告会在游戏的底端显示


三、加入插屏广告

1、在类AppActivity同一包名下新建ShowAdHelper类文件

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(){
@Override
publicvoidonAdClick(InterstitialAdarg0){
Log.i("InterstitialAd","onAdClick");
}
@Override
publicvoidonAdDismissed(){
Log.i("InterstitialAd","onAdDismissed");
interAd1.loadAd();
}
@Override
publicvoidonAdFailed(Stringarg0){
Log.i("InterstitialAd","onAdFailed");
}
@Override
publicvoidonAdPresent(){
Log.i("InterstitialAd","onAdPresent");
}
@Override
publicvoidonAdReady(){
Log.i("InterstitialAd","onAdReady");
}
});
interAd1.loadAd();
returninterAd1;
}

4、创建Handler

privateHandleraHandler=newHandler(){
@Override
publicvoidhandleMessage(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加入代码

@Override
protectedvoidonCreate(BundlesavedInstanceState){
//TODOAuto-generatedmethodstub
super.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相关文章