说一下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>下。

  1. <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> 
  2. <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

将以下标签添加到<manifest>下。

  1. <uses-permission android:name="android.permission.INTERNET"/> 
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

由于Cocos2d-x v3.0使用android.app.NativeActivity,替代了原先的android.app.Activity,因此就有一个问题,Admob不能很好地工作在纯native activity应用中。以下为解决方案。
好了,打开Cocos2dxActivity.java,按如下方式修改:

  1. package org.cocos2dx.cpp;
  2. import android.app.NativeActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.Gravity;
  6. import android.view.ViewGroup.LayoutParams;
  7. import android.view.ViewGroup.MarginLayoutParams;
  8. import android.view.WindowManager;
  9. import android.widget.LinearLayout;
  10. import android.widget.PopupWindow;
  11. import com.google.android.gms.ads.AdListener;
  12. import com.google.android.gms.ads.AdRequest;
  13. import com.google.android.gms.ads.AdSize;
  14. import com.google.android.gms.ads.AdView;
  15. public class Cocos2dxActivity extends NativeActivity {
  16. private AdView adView;
  17. private PopupWindow popUp;
  18. private static Cocos2dxActivity _activity;
  19. private LinearLayout layout;
  20. private LinearLayout mainLayout;
  21. boolean adsinited = false;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. // Make your custom init here
  26. getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  27. _activity = this;
  28. // Create our ad view here
  29. adView = new AdView(this); // you can get the ID from Admob
  30. adView.setAdUnitId("a1531054c66348c");
  31. adView.setAdSize(AdSize.BANNER);
  32. }
  33. public static void showAdPopup(){
  34. _activity._showAdPopup();
  35. }
  36. public void _showAdPopup() {
  37. if (adsinited) {
  38. return;
  39. }
  40. if (adView != null) {
  41. _activity.runOnUiThread(new Runnable() {
  42. @Override
  43. public void run() {
  44. adsinited = true;
  45. // Out popup window
  46. popUp = new PopupWindow(_activity);
  47. // 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)
  48. popUp.setWidth(320);
  49. popUp.setHeight(50);
  50. popUp.setWindowLayoutMode(LayoutParams.WRAP_CONTENT,
  51. LayoutParams.WRAP_CONTENT);
  52. popUp.setClippingEnabled(false);
  53. layout = new LinearLayout(_activity);
  54. mainLayout = new LinearLayout(_activity);
  55. // 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
  56. layout.setPadding(-5, -5, -5, -5);
  57. MarginLayoutParams params = new MarginLayoutParams(
  58. LayoutParams.WRAP_CONTENT,
  59. LayoutParams.WRAP_CONTENT);
  60. params.setMargins(0, 0, 0, 0);
  61. layout.setOrientation(LinearLayout.VERTICAL);
  62. layout.addView(adView, params);
  63. popUp.setContentView(layout);
  64. _activity.setContentView(mainLayout, params);// you can get the TestDevice ID from the output of logcat .
  65. AdRequest adRequest = new AdRequest.Builder()
  66. .addTestDevice(
  67. "CE7DCE5945F79BBF863872D5026787EFbuild")
  68. .build();
  69. // Enable this if your are testing AdMob, otherwise you'll risk to be banned!
  70. _activity.adView.loadAd(adRequest);
  71. // Show our popup window
  72. popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 0, 0);
  73. popUp.update();
  74. }
  75. });
  76. }
  77. }
  78. private class AdmobListener extends AdListener {
  79. @Override
  80. public void onAdClosed() {
  81. super.onAdClosed();
  82. }
  83. @Override
  84. public void onAdFailedToLoad(int errorCode) {
  85. super.onAdFailedToLoad(errorCode);
  86. }
  87. @Override
  88. public void onAdLeftApplication() {
  89. super.onAdLeftApplication();
  90. }
  91. @Override
  92. public void onAdLoaded() {
  93. super.onAdLoaded();
  94. }
  95. @Override
  96. public void onAdOpened() {
  97. super.onAdOpened();
  98. }
  99. }
  100. @Override
  101. public void onDestroy() {
  102. if (adView != null) {
  103. adView.destroy();
  104. }
  105. super.onDestroy();
  106. }
  107. }

  108. 重要提示: 
  109. 要谨记的是不要在onCreate回调中调用这个函数,必须要在整个窗口已经创建完毕后才能调用这段代码,就这里而言,就是当游戏窗口已经准备要显示的时候调用。
  110. 创建AdmobHelper.h,加入以下代码:
  111. 复制代码
  112. #ifndef  __Admob_Helper_H_
  113. #define  __Admob_Helper_H_
  114. class AdmobHelper
  115. {
  116. public:
  117.     static void showAds();
  118. //if necessary, you can add other methods to control AdView(e.g. dismiss the AdView).
  119. };
  120. #endif  //__INTERFACE_FACEBOOK_H_

  121. 创建AdmobHelper.cpp,加入以下代码:
  122. 复制代码
  123. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  124. #include "AdmobHelper.h"
  125. #include "platform/android/jni/JniHelper.h"
  126. #include <jni.h>
  127. #include <android/log.h>
  128. const char* NativeActivityClassName = "org/cocos2dx/cpp/Cocos2dxActivity";
  129. void AdmobHelper::showAds(){
  130. cocos2d::JniMethodInfo t;
  131. if (cocos2d::JniHelper::getStaticMethodInfo(t
  132.                                                 , NativeActivityClassName
  133.                                                 , "showAdPopup"
  134.                                                 , "()V"))
  135. {
  136.   t.env->CallStaticVoidMethod(t.classID, t.methodID);
  137.   t.env->DeleteLocalRef(t.classID);
  138. }
  139. }
  140. #endif

最后,你可以在C++端通过包含“AdmobHelper.h”,然后调用AdmobHelper::showAds()就可以看到广告了。