一、主要步骤

  • 创建一个免费的Admob账户: (http://www.google.com/ads/admob/39)[http://www.google.com/ads/admob/39]
  • 创建一个新的app ID,并设置类型为插页广告的AdMob广告单元
  • 导入Google Play services,在eclipse中选择Windows -> Android SDK Manager -> Extras -> “Google Play services” -> Install packages
  • 修改AndroidManifest.xml文件新增如下代码:

AndroidManifest.xml

<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"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  • 修改AppActivity.java类如下:

AppActivity.java

public class AppActivity extends Cocos2dxActivity {

private static AppActivity _appActiviy;
private static final String AD_UNIT_ID = "your-ad-unit-id"; //你自己广告单元的ID
private static InterstitialAd interstitial;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId(AD_UNIT_ID);
    _appActiviy = this;
    AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("HASH_DEVICE_ID")
    .build();

    interstitial.loadAd(adRequest);

}
public static void showInterstitial(){
    _appActiviy.runOnUiThread(new Runnable() {

        @Override
        public void run() {
             if (interstitial.isLoaded()) {
                  interstitial.show();
              }
        }
    });

    }

}
  • 最后添加cpp代码,这与横幅广告一样:

AdmobHelper.h

#ifndef  __ADMOB_HELPER_H_
#define  __ADMOB_HELPER_H_

class AdmobHelper
{
public:
    static void hideAd();
    static void showAd();
    static bool isAdShowing;

};
#endif // __ADMOB_HELPER_H_

AdmobHelper.cpp

#include "AdmobHelper.h"
#include "cocos2d.h"

bool AdmobHelper::isAdShowing = true;

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

#include "platform/android/jni/JniHelper.h"
#include <jni.h>
//#include <android/log.h>


const char* AppActivityClassName = "org/cocos2dx/cpp/AppActivity";

void AdmobHelper::hideAd()
{
    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, AppActivityClassName, "hideAd", "()V"))
    {

        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
        isAdShowing = false;
    }
}



void AdmobHelper::showAd()
{

    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, AppActivityClassName, "showAd", "()V"))
    {

        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
        isAdShowing = true;
    }

}


#else


void AdmobHelper::hideAd()
{
    CCLOG("hideAd() called");
    isAdShowing = false;
    return; //nothing
}


void AdmobHelper::showAd()
{
    CCLOG("showAd() called");
    isAdShowing = true;
    return; //nothing;

}

#endif

总结

简单讲述cocos2d-x-3在android中添加插页广告的步骤。

##文档信息