您好,登錄后才能下訂單哦!
用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()函數即可
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。