说一下cocos2dx 3.x admob的详细流程。
1.在Admob中创建app
首先,创建一个免费的Admob账户,提供如下信息来完成Abmob账户的创建。
然后,添加你的第一个app,如下:
2.使用Google Play Service SDK
因为Google Play将不再接受使用Google Mobile Ads SDK v6.4.1以及更低版本的创建新应用和更新应用,因此我们选择了Android(Google Play Services),这也是官方推荐在Android应用中使用广告的方式。
3.配置Google Play Services SDK
在使用Google Play services APIs开发应用之前,我们需要先从SDK Manager下载Google Play services SDK。
-
打开SDK Manager: 在已安装ADT的Eclipse中,选择Window -> Android SDK Manager
-
安装Google Play services SDK: 滚动Package list至底部,展开Extras,选择Google Play services,确认安装。它将会把Android SDK环境保存到$ANDROID_SDK/extras/google/google_play_services/。
-
拷贝Google Play services库工程: 拷贝$ANDROID_SDK/extras/google/google_play_services/libproject/google-play-services_lib/库项目到你需要维护的安卓项目中。
4.集成SDK
添加并引用Google Play services库工程。在Eclipse中右键点击项目选择"Properties"。
选择“Android”,点击“Add...”,找到google-play-services_lib工程,点击“OK”来添加Google Play services库。
项目现在就已经引用了Google Play services库。
打开AndroidManifest.xml,将以下标签添加到<application>下。
- <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
- <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
将以下标签添加到<manifest>下。
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
由于Cocos2d-x v3.0使用android.app.NativeActivity,替代了原先的android.app.Activity,因此就有一个问题,Admob不能很好地工作在纯native activity应用中。以下为解决方案。
好了,打开Cocos2dxActivity.java,按如下方式修改:
- package org.cocos2dx.cpp;
- import android.app.NativeActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.ViewGroup.LayoutParams;
- import android.view.ViewGroup.MarginLayoutParams;
- import android.view.WindowManager;
- import android.widget.LinearLayout;
- import android.widget.PopupWindow;
- import com.google.android.gms.ads.AdListener;
- import com.google.android.gms.ads.AdRequest;
- import com.google.android.gms.ads.AdSize;
- import com.google.android.gms.ads.AdView;
- public class Cocos2dxActivity extends NativeActivity {
- private AdView adView;
- private PopupWindow popUp;
- private static Cocos2dxActivity _activity;
- private LinearLayout layout;
- private LinearLayout mainLayout;
- boolean adsinited = false;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // Make your custom init here
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- _activity = this;
- // Create our ad view here
- adView = new AdView(this); // you can get the ID from Admob
- adView.setAdUnitId("a1531054c66348c");
- adView.setAdSize(AdSize.BANNER);
- }
- public static void showAdPopup(){
- _activity._showAdPopup();
- }
- public void _showAdPopup() {
- if (adsinited) {
- return;
- }
- if (adView != null) {
- _activity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- adsinited = true;
- // Out popup window
- popUp = new PopupWindow(_activity);
- // This is the minimum size for AdMob, we need to set this in case our target device run at 320x480 resolution(Otherwise no ad will be shown, see the padding kill below)
- popUp.setWidth(320);
- popUp.setHeight(50);
- popUp.setWindowLayoutMode(LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT);
- popUp.setClippingEnabled(false);
- layout = new LinearLayout(_activity);
- mainLayout = new LinearLayout(_activity);
- // The layout system for the PopupWindow will kill some pixels due to margins/paddings etc… (No way to remove it), so padd it to adjust
- layout.setPadding(-5, -5, -5, -5);
- MarginLayoutParams params = new MarginLayoutParams(
- LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT);
- params.setMargins(0, 0, 0, 0);
- layout.setOrientation(LinearLayout.VERTICAL);
- layout.addView(adView, params);
- popUp.setContentView(layout);
- _activity.setContentView(mainLayout, params);// you can get the TestDevice ID from the output of logcat .
- AdRequest adRequest = new AdRequest.Builder()
- .addTestDevice(
- "CE7DCE5945F79BBF863872D5026787EFbuild")
- .build();
- // Enable this if your are testing AdMob, otherwise you'll risk to be banned!
- _activity.adView.loadAd(adRequest);
- // Show our popup window
- popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 0, 0);
- popUp.update();
- }
- });
- }
- }
- private class AdmobListener extends AdListener {
- @Override
- public void onAdClosed() {
- super.onAdClosed();
- }
- @Override
- public void onAdFailedToLoad(int errorCode) {
- super.onAdFailedToLoad(errorCode);
- }
- @Override
- public void onAdLeftApplication() {
- super.onAdLeftApplication();
- }
- @Override
- public void onAdLoaded() {
- super.onAdLoaded();
- }
- @Override
- public void onAdOpened() {
- super.onAdOpened();
- }
- }
- @Override
- public void onDestroy() {
- if (adView != null) {
- adView.destroy();
- }
- super.onDestroy();
- }
- }
-
- 重要提示:
- 要谨记的是不要在onCreate回调中调用这个函数,必须要在整个窗口已经创建完毕后才能调用这段代码,就这里而言,就是当游戏窗口已经准备要显示的时候调用。
- 创建AdmobHelper.h,加入以下代码:
- 复制代码
- #ifndef __Admob_Helper_H_
- #define __Admob_Helper_H_
- class AdmobHelper
- {
- public:
- static void showAds();
- //if necessary, you can add other methods to control AdView(e.g. dismiss the AdView).
- };
- #endif //__INTERFACE_FACEBOOK_H_
-
- 创建AdmobHelper.cpp,加入以下代码:
- 复制代码
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- #include "AdmobHelper.h"
- #include "platform/android/jni/JniHelper.h"
- #include <jni.h>
- #include <android/log.h>
- const char* NativeActivityClassName = "org/cocos2dx/cpp/Cocos2dxActivity";
- void AdmobHelper::showAds(){
- cocos2d::JniMethodInfo t;
- if (cocos2d::JniHelper::getStaticMethodInfo(t
- , NativeActivityClassName
- , "showAdPopup"
- , "()V"))
- {
- t.env->CallStaticVoidMethod(t.classID, t.methodID);
- t.env->DeleteLocalRef(t.classID);
- }
- }
- #endif
最后,你可以在C++端通过包含“AdmobHelper.h”,然后调用AdmobHelper::showAds()就可以看到广告了。