转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/35986939
DEMO下载地址:http://download.csdn.net/detail/oyangyufu/7578451
在经过国内的几个广告平台尝试,最终选择芒果广告,有几个优点:SDK更新较快,支持cocos2d-x3.0,平台还聚合了国内主流广告平台,下载相应的SDK也可加载到工程中。
芒果横幅广告测试
1、将cocos2d-x项目导入到eclipse上
2、下载android SDK cocos2d-x 版本,网址:http://my.adsmogo.com/Apps/Download,下载完名称为:AdsMogo_Android_Cocos2d-x_Demo.zip
3、解压后将jar包放入libs目录下,单击右键->Build Path->Add to Build Path
4、在AndroidManifest.xml添加用户权限:
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
- <uses-permission android:name="android.permission.CALL_PHONE" />
- <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.VIBRATE" />
- <uses-permission android:name="android.permission.RECORD_AUDIO" />
- <uses-permission android:name="android.permission.GET_TASKS" />
- <uses-permission android:name="android.permission.CAMERA" />
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
- <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
5、声明广告平台Activity, 代码到AndroidMainfest.xml文件中的application标签中
- <pre class="cpp" name="code"> <activity
- android:name="com.adsmogo.adview.AdsMogoWebView"
- android:configChanges="orientation|keyboardHidden|screenSize"
- android:label="@string/app_name" >
- </activity>
- <!-- 注册Service,自主广告与交换广告的应用下载功能所需 -->
- <service android:name="com.adsmogo.controller.service.UpdateService"
- android:exported="true"
- android:process=":remote" />
- <!-- 注册Service,安装应用所用 -->
- <service android:name="com.adsmogo.controller.service.CountService"
- android:exported="true"
- android:process=":remote" />
6、在主Activity类中,添加横幅广告
- public class Test extends Cocos2dxActivity{
- private static Handler handler;
- private static RelativeLayout bannerLayout;
- private AdsMogoLayout adView;
-
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- bannerLayout = new RelativeLayout(this);
- RelativeLayout.LayoutParams parentLayputParams = new RelativeLayout.LayoutParams(
- RelativeLayout.LayoutParams.FILL_PARENT,
- RelativeLayout.LayoutParams.FILL_PARENT);
- this.addContentView(bannerLayout, parentLayputParams);
-
-
-
-
- AdsMogoInterstitialManager.setDefaultInitAppKey("93535c6092f543e8a257ee435a69da06");
- AdsMogoInterstitialManager.setInitActivity(Test.this);
- AdsMogoInterstitialManager.shareInstance()
- .initDefaultInterstitial();
- AdsMogoInterstitialManager.shareInstance()
- .defaultInterstitial()
- .setAdsMogoInterstitialListener(new AdsMogoInterstitialListener() {
-
- @Override
- public void onShowInterstitialScreen(String arg0) {
-
- Log.v("MogoCocos2dx Demo", "onShowInterstitialScreen");
- }
-
- @Override
- public boolean onInterstitialStaleDated(String arg0) {
-
- Log.v("MogoCocos2dx Demo", "onInterstitialStaleDated");
- return false;
- }
-
- @Override
- public void onInterstitialRealClickAd(String arg0) {
-
- Log.v("MogoCocos2dx Demo", "onInterstitialRealClickAd");
- }
-
- @Override
- public View onInterstitialGetView() {
-
- Log.v("MogoCocos2dx Demo", "onInterstitialGetView");
- return bannerLayout;
- }
-
- @Override
- public void onInterstitialCloseAd(boolean arg0) {
-
- Log.v("MogoCocos2dx Demo", "onInterstitialCloseAd");
- }
-
- @Override
- public boolean onInterstitialClickCloseButton() {
-
- Log.v("MogoCocos2dx Demo", "onInterstitialClickCloseButton");
- return false;
- }
-
- @Override
- public void onInterstitialClickAd(String arg0) {
-
- Log.v("MogoCocos2dx Demo", "onInterstitialClickAd");
- }
-
- @Override
- public Class getCustomEvemtPlatformAdapterClass(
- AdsMogoCustomEventPlatformEnum arg0) {
-
- return null;
- }
- });
-
-
- handler = new Handler(){
-
- @Override
- public void handleMessage(Message msg) {
-
- switch (msg.what) {
- case 0:
-
- if (bannerLayout.getChildCount() == 0) {
- adView = new AdsMogoLayout(Test.this,
- "46184ad32a0c417887cb433203b11403");
- adView.setAdsMogoListener(new AdsMogoListener() {
-
- @Override
- public void onRequestAd(String arg0) {
-
- Log.v("MogoCocos2dx Demo", "onRequestAd");
- }
-
- @Override
- public void onReceiveAd(ViewGroup arg0, String arg1) {
-
- Log.v("MogoCocos2dx Demo", "onReceiveAd");
- }
-
- @Override
- public void onRealClickAd() {
-
- Log.v("MogoCocos2dx Demo", "onRealClickAd");
- }
-
- @Override
- public void onFailedReceiveAd() {
-
- Log.v("MogoCocos2dx Demo", "onFailedReceiveAd");
- }
-
- @Override
- public void onCloseMogoDialog() {
-
- Log.v("MogoCocos2dx Demo", "onCloseMogoDialog");
- }
-
- @Override
- public boolean onCloseAd() {
-
- return false;
- }
-
- @Override
- public void onClickAd(String arg0) {
-
- Log.v("MogoCocos2dx Demo", "onClickAd");
- }
-
- @Override
- public Class getCustomEvemtPlatformAdapterClass(
- AdsMogoCustomEventPlatformEnum arg0) {
-
- return null;
- }
- });
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
- RelativeLayout.LayoutParams.FILL_PARENT,
- RelativeLayout.LayoutParams.WRAP_CONTENT);
- layoutParams.addRule(
- RelativeLayout.ALIGN_PARENT_BOTTOM,
- RelativeLayout.TRUE);
- bannerLayout.addView(adView, layoutParams);
- }
- break;
- case 1:
- onClickHideShow();
- break;
- case 3:
-
-
-
-
-
-
- AdsMogoInterstitialManager.shareInstance()
- .defaultInterstitial().interstitialShow(true);
- break;
- case 4:
- Test.this.finish();
- break;
- case 5:
-
-
-
-
-
- AdsMogoInterstitialManager.shareInstance()
- .defaultInterstitial().interstitialCancel();
- break;
- }
- }
- };
- }
-
- public Cocos2dxGLSurfaceView onCreateView() {
- Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
-
- glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
-
- return glSurfaceView;
- }
-
-
- public static void showBannerStatic() {
- Message msg = handler.obtainMessage();
- msg.what = 0;
- handler.sendMessage(msg);
- }
-
- public static void hideBannerStatic() {
- Message msg = handler.obtainMessage();
- msg.what = 1;
- handler.sendMessage(msg);
- }
-
- public static void showInterstitialStatic() {
- Message msg = handler.obtainMessage();
- msg.what = 3;
- handler.sendMessage(msg);
- }
- public static void close() {
- Message msg = handler.obtainMessage();
- msg.what = 4;
- handler.sendMessage(msg);
- }
-
- public static void closeInterstitial() {
- Message msg = handler.obtainMessage();
- msg.what = 5;
- handler.sendMessage(msg);
- }
-
-
- static {
- System.loadLibrary("cocos2dcpp");
- }
- public void onClickHideShow() {
- if (adView != null) {
- adView.setVisibility(adView.getVisibility() == View.VISIBLE ? View.GONE
- : View.VISIBLE);
- }
- }
- }
7、CCP程序中jni调用java函数,显示广告
定义方法:
- #include "MOGOAd.h"
- MOGOAd::MOGOAd(){}
- MOGOAd::~MOGOAd(){}
- void MOGOAd::showBanner()
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- JniMethodInfo showBanner;
- bool isHave = JniHelper::getStaticMethodInfo(showBanner,"com/cocos2dx/org/Test","showBannerStatic","()V");
- if (!isHave) {
- CCLog("jni:showBannerStatic false");
- }else{
-
- showBanner.env->CallStaticVoidMethod(showBanner.classID, showBanner.methodID);
- }
- #endif
- }
- void MOGOAd::hideBanner()
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- JniMethodInfo hideBanner;
- bool isHave = JniHelper::getStaticMethodInfo(hideBanner,"com/cocos2dx/org/Test","hideBannerStatic","()V");
- if (!isHave) {
- CCLog("jni:hideBannerStatic false");
- }else{
- CCLog("jni:hideBannerStatic true");
-
- hideBanner.env->CallStaticVoidMethod(hideBanner.classID, hideBanner.methodID);
- }
- #endif
- }
-
- void MOGOAd::showInterstitial()
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- JniMethodInfo showInterstitial;
- bool isHave = JniHelper::getStaticMethodInfo(showInterstitial,"com/cocos2dx/org/Test","showInterstitialStatic","()V");
- if (!isHave) {
- CCLog("jni:hideBannerStatic false");
- }else{
- CCLog("jni:hideBannerStatic true");
-
- showInterstitial.env->CallStaticVoidMethod(showInterstitial.classID, showInterstitial.methodID);
- }
- #endif
- }
- void MOGOAd::close()
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- JniMethodInfo showInterstitial;
- bool isHave = JniHelper::getStaticMethodInfo(showInterstitial,"org/cocos2dx/cpp/AdsMogoCoCos2dx","close","()V");
- if (!isHave) {
- CCLog("jni:hideBannerStatic false");
- }else{
- CCLog("jni:hideBannerStatic true");
-
- showInterstitial.env->CallStaticVoidMethod(showInterstitial.classID, showInterstitial.methodID);
- }
- #endif
- }
- void MOGOAd::closeInterstitial()
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- JniMethodInfo closeInterstitial;
- bool isHave = JniHelper::getStaticMethodInfo(closeInterstitial,"org/cocos2dx/cpp/AdsMogoCoCos2dx","closeInterstitial","()V");
- if (!isHave) {
- CCLog("jni:hideBannerStatic false");
- }else{
- CCLog("jni:hideBannerStatic true");
-
- closeInterstitial.env->CallStaticVoidMethod(closeInterstitial.classID, closeInterstitial.methodID);
- }
- #endif
- }
-
8、创建菜单按钮,点击显示广告
- CCSize winSize = CCDirector::sharedDirector()->getWinSize();
- CCMenuItemLabel * bannerLable=CCMenuItemLabel::create(CCLabelTTF::create("show banner", "Helvetica", 30),this, menu_selector(HelloWorld::menuBannerCallback));
- bannerLable->setPosition(ccp(winSize.width -200, winSize.height -80));
- void HelloWorld::menuCloseCallback(CCObject *pSender)
- {
- MOGOAd::close();
- }
- void HelloWorld::menuBannerCallback(CCObject *pSender){
- MOGOAd::showBanner();
- }
- void HelloWorld::menuRequestInterstitialCallback(CCObject *pSender){
- MOGOAd::hideBanner();
- }
- void HelloWorld::menushowInterstitialCallback(CCObject *pSender){
- MOGOAd::showInterstitial();
- }
- void HelloWorld::menucloseInterstitalCallback(CCObject *pSender){
- MOGOAd::closeInterstitial();
- }
9、效果图