用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包,如下图所示:
2、配置AndroidManifest.xml
◆添加权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <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.RECORD_AUDIO" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
◆添加APPSID和AppActivity申明
<meta-data android:name="BaiduMobAd_APP_ID" android:value="dcd69c04" /> <meta-data android:name="BaiduMobAd_APP_SEC" android:value="dcd69c04" /> <activity android:name="com.baidu.mobads.AppActivity" android:configChanges="keyboard|keyboardHidden|orientation" />
如下图所示:
二、横幅banner广告的接入
在AppActivity.java文件中加入如下代码
private AdView adView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setupAds(); } private void setupAds() { RelativeLayout layout = new RelativeLayout(this); addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); adView = new AdView(this); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); layout.addView(adView, layoutParams); }
如图所示:
加入代码后广告会在游戏的底端显示
三、加入插屏广告
1、在类AppActivity同一包名下新建ShowAdHelper类文件
2、在类ShowAdHelper中加入如下代码
public class ShowAdHelper { private static Handler mHandler; public static void init(Handler handler) { ShowAdHelper.mHandler = handler; } private static void showMyAd() { Message msg = mHandler.obtainMessage(); msg.what = AppActivity.SHOW_AD; msg.sendToTarget(); } }
3、在类ShowAdHelper中定义一个变量和一个常量
public static final int SHOW_AD = 0x0001; InterstitialAd interAd;
3、在类AppActivity中新建getInsertAd()方法初始化插屏广告
public InterstitialAd getInsertAd() { final InterstitialAd interAd1 = new InterstitialAd(this); interAd1.setListener(new InterstitialAdListener() { @Override public void onAdClick(InterstitialAd arg0) { Log.i("InterstitialAd", "onAdClick"); } @Override public void onAdDismissed() { Log.i("InterstitialAd", "onAdDismissed"); interAd1.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"); } }); interAd1.loadAd(); return interAd1; }
4、创建Handler
private Handler aHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case SHOW_AD: if (interAd.isAdReady()) { interAd.showAd(AppActivity.this); Log.d("LC", "show"); } else { interAd.loadAd(); } Log.d("LC", "not show"); break; } } };
5、在onCreate加入代码
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); interAd = getInsertAd(); ShowAdHelper.init(aHandler); }
6、java层的编写就此结束了,接下去在C++层中通过jni调用ShowAdHelper类中的showMyAd函数
void BackgroundLayer::loadAndroidAd() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台 JniMethodInfo minfo; jobject jobj; bool b = 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()函数即可