cocos2d-x 3.X 在android 中添加多盟插屏广告

效果如下



注册和添加应用就不说了

android部分

1.先将SDK放到libs中



2.AndroidManifest.xml修改如下

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="duomengad.crlyn"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.   
  7.     <uses-sdk android:minSdkVersion="16"/>  
  8.     <!-- 网络访问权限 -->  
  9.     <uses-permission android:name="android.permission.INTERNET" />  
  10.     <!-- 获取网络信息状态,如当前的网络连接是否有效 -->  
  11.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  12.     <!-- 读取手机状态 -->  
  13.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  14.     <!-- 允许程序写入外部存储,如SD卡上写文件 -->  
  15.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  16.     <!-- 获取错略位置 -->  
  17.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  18.     <!-- 获取WiFi状态 -->  
  19.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  20.       
  21.     <!-- 以下权限为可选权限,主要是品牌广告会用到,如果您没有添加,需要这些效果的品牌广告将不会投放到您的应用上 -->  
  22.     <!-- 震动权限 -->  
  23.     <uses-permission android:name="android.permission.VIBRATE" />  
  24.     <uses-feature android:glEsVersion="0x00020000" />  
  25.   
  26.     <application android:label="@string/app_name"  
  27.                  android:icon="@drawable/icon">  
  28.                        
  29.         <!-- Tell Cocos2dxActivity the name of our .so -->  
  30.         <meta-data android:name="android.app.lib_name"  
  31.                   android:value="cocos2dcpp" />  
  32.         <!-- 多盟的SDK -->  
  33.         <activity android:name="cn.domob.android.ads.DomobActivity"  
  34.             android:theme="@android:style/Theme.Translucent"></activity>  
  35.         <activity android:name="org.cocos2dx.cpp.AppActivity"  
  36.                   android:label="@string/app_name"  
  37.                   android:screenOrientation="portrait"  
  38.                   android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  
  39.                   android:configChanges="orientation">  
  40.   
  41.             <intent-filter>  
  42.                 <action android:name="android.intent.action.MAIN" />  
  43.                 <category android:name="android.intent.category.LAUNCHER" />  
  44.             </intent-filter>  
  45.         </activity>  
  46.     </application>  
  47.   
  48.     <supports-screens android:anyDensity="true"  
  49.                       android:smallScreens="true"  
  50.                       android:normalScreens="true"  
  51.                       android:largeScreens="true"  
  52.                       android:xlargeScreens="true"/>  
  53.   
  54.     <uses-permission android:name="android.permission.INTERNET"/>  
  55. </manifest>   

3.AppActivity.java 文件修改如下

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**************************************************************************** 
  2. Copyright (c) 2008-2010 Ricardo Quesada 
  3. Copyright (c) 2010-2012 cocos2d-x.org 
  4. Copyright (c) 2011      Zynga Inc. 
  5. Copyright (c) 2013-2014 Chukong Technologies Inc. 
  6.   
  7. http://www.cocos2d-x.org 
  8.  
  9. Permission is hereby granted, free of charge, to any person obtaining a copy 
  10. of this software and associated documentation files (the "Software"), to deal 
  11. in the Software without restriction, including without limitation the rights 
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
  13. copies of the Software, and to permit persons to whom the Software is 
  14. furnished to do so, subject to the following conditions: 
  15.  
  16. The above copyright notice and this permission notice shall be included in 
  17. all copies or substantial portions of the Software. 
  18.  
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  20. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  21. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
  22. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  23. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
  24. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
  25. THE SOFTWARE. 
  26. ****************************************************************************/  
  27. package org.cocos2dx.cpp;  
  28.   
  29. import org.cocos2dx.lib.Cocos2dxActivity;  
  30.   
  31. import cn.domob.android.ads.DomobAdManager.ErrorCode;  
  32. import cn.domob.android.ads.DomobInterstitialAd;  
  33. import cn.domob.android.ads.DomobInterstitialAdListener;  
  34. import android.os.Bundle;  
  35. import android.util.Log;  
  36.   
  37. public class AppActivity extends Cocos2dxActivity {  
  38.       
  39.     private DomobInterstitialAd mInterstitialAd;  
  40.     private static AppActivity _AppActivity;  
  41.     public static final String PUBLISHER_ID = "56OJwdBIuNBzc/kIO9";  
  42.     public static final String InterstitialPPID = "16TLuqBoApj34NUE2GW-0G9z";  
  43.       
  44.     @Override  
  45.     protected void onCreate(Bundle savedIntanceState){  
  46.         super.onCreate(savedIntanceState);  
  47.           
  48.         mInterstitialAd = new DomobInterstitialAd(this, PUBLISHER_ID, InterstitialPPID, DomobInterstitialAd.INTERSITIAL_SIZE_300X250);  
  49.           
  50.         mInterstitialAd.setInterstitialAdListener(new DomobInterstitialAdListener() {  
  51.             @Override  
  52.             public void onInterstitialAdReady() {  
  53.                 Log.i("DomobSDKDemo""onAdReady");  
  54.             }  
  55.   
  56.             @Override  
  57.             public void onLandingPageOpen() {  
  58.                 Log.i("DomobSDKDemo""onLandingPageOpen");  
  59.             }  
  60.   
  61.             @Override  
  62.             public void onLandingPageClose() {  
  63.                 Log.i("DomobSDKDemo""onLandingPageClose");  
  64.             }  
  65.   
  66.             @Override  
  67.             public void onInterstitialAdPresent() {  
  68.                 Log.i("DomobSDKDemo""onInterstitialAdPresent");  
  69.             }  
  70.   
  71.             @Override  
  72.             public void onInterstitialAdDismiss() {  
  73.                 // Request new ad when the previous interstitial ad was closed.  
  74.                 mInterstitialAd.loadInterstitialAd();  
  75.                 Log.i("DomobSDKDemo""onInterstitialAdDismiss");  
  76.             }  
  77.   
  78.             @Override  
  79.             public void onInterstitialAdFailed(ErrorCode arg0) {  
  80.                 Log.i("DomobSDKDemo""onInterstitialAdFailed");                  
  81.             }  
  82.   
  83.             @Override  
  84.             public void onInterstitialAdLeaveApplication() {  
  85.                 Log.i("DomobSDKDemo""onInterstitialAdLeaveApplication");  
  86.                   
  87.             }  
  88.   
  89.             @Override  
  90.             public void onInterstitialAdClicked(DomobInterstitialAd arg0) {  
  91.                 Log.i("DomobSDKDemo""onInterstitialAdClicked");  
  92.             }  
  93.         });  
  94.           
  95.         mInterstitialAd.loadInterstitialAd();  
  96.         _AppActivity = this;  
  97.     }  
  98.       
  99.     public static void showAd(){  
  100.         _AppActivity.runOnUiThread(new Runnable() {  
  101.             @Override  
  102.             public void run() {  
  103.                 if (_AppActivity.mInterstitialAd.isInterstitialAdReady()){  
  104.                     _AppActivity.mInterstitialAd.showInterstitialAd(_AppActivity);  
  105.                 } else {  
  106.                     Log.i("DomobSDKDemo""Interstitial Ad is not ready");  
  107.                     _AppActivity.mInterstitialAd.loadInterstitialAd();  
  108.                 }  
  109.             }  
  110.         });  
  111.     }  
  112. }  

4.android.mk修改如下

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. LOCAL_PATH := $(call my-dir)  
  2.   
  3. include $(CLEAR_VARS)  
  4.   
  5. LOCAL_MODULE := cocos2dcpp_shared  
  6.   
  7. LOCAL_MODULE_FILENAME := libcocos2dcpp  
  8.   
  9. LOCAL_SRC_FILES := hellocpp/main.cpp \  
  10.                    ../../Classes/AppDelegate.cpp \  
  11.                    ../../Classes/HelloWorldScene.cpp \  
  12.                    ../../Classes/Admob.cpp  
  13.   
  14. LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes  
  15.   
  16. LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static  
  17. LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static  
  18.   
  19. # LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static  
  20. # LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static  
  21. # LOCAL_WHOLE_STATIC_LIBRARIES += spine_static  
  22. # LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static  
  23. # LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static  
  24. # LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static  
  25.   
  26.   
  27. include $(BUILD_SHARED_LIBRARY)  
  28.   
  29. $(call import-module,.)  
  30. $(call import-module,audio/android)  
  31.   
  32. # $(call import-module,Box2D)  
  33. # $(call import-module,editor-support/cocosbuilder)  
  34. # $(call import-module,editor-support/spine)  
  35. # $(call import-module,editor-support/cocostudio)  
  36. # $(call import-module,network)  
  37. # $(call import-module,extensions)  

VS部分

1.新建Admob.h

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #pragma once  
  2.   
  3. class Admob{  
  4. public:  
  5.     static void showAd();  
  6. };  

2.新建Admob.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "Admob.h"  
  2. #include "cocos2d.h"  
  3.   
  4. USING_NS_CC;  
  5.   
  6. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  
  7. #include "platform/android/jni/JniHelper.h"  
  8. #include <jni.h>  
  9.   
  10. const char* AppActivityCalssName = "org/cocos2dx/cpp/AppActivity";  
  11.   
  12. void Admob::showAd(){  
  13.     cocos2d::JniMethodInfo t;  
  14.     if(cocos2d::JniHelper::getStaticMethodInfo(t,AppActivityCalssName,"showAd","()V")){  
  15.         t.env->CallStaticVoidMethod(t.classID,t.methodID);  
  16.         t.env->DeleteLocalRef(t.classID);  
  17.     }  
  18. }  
  19.   
  20. #else  
  21.   
  22. void Admob::showAd(){  
  23.     log("showAd() called");  
  24.     return;  
  25. }  
  26.   
  27. #endif  

3.修改HelloWorldScene.h文件,修改如下

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef __HELLOWORLD_SCENE_H__  
  2. #define __HELLOWORLD_SCENE_H__  
  3.   
  4. #include "cocos2d.h"  
  5.   
  6. class HelloWorld : public cocos2d::Layer  
  7. {  
  8. public:  
  9.     // there's no 'id' in cpp, so we recommend returning the class instance pointer  
  10.     static cocos2d::Scene* createScene();  
  11.   
  12.     // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone  
  13.     virtual bool init();    
  14.       
  15.     // a selector callback  
  16.     void menuCloseCallback(cocos2d::Ref* pSender);  
  17.     void adCallback(cocos2d::Ref* pRef);  
  18.       
  19.     // implement the "static create()" method manually  
  20.     CREATE_FUNC(HelloWorld);  
  21. };  
  22.   
  23. #endif // __HELLOWORLD_SCENE_H__  

4.修改HelloWorldScene.cpp文件,修改如下

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "HelloWorldScene.h"  
  2. #include "Admob.h"  
  3.   
  4. USING_NS_CC;  
  5.   
  6. Scene* HelloWorld::createScene()  
  7. {  
  8.     // 'scene' is an autorelease object  
  9.     auto scene = Scene::create();  
  10.       
  11.     // 'layer' is an autorelease object  
  12.     auto layer = HelloWorld::create();  
  13.   
  14.     // add layer as a child to scene  
  15.     scene->addChild(layer);  
  16.   
  17.     // return the scene  
  18.     return scene;  
  19. }  
  20.   
  21. // on "init" you need to initialize your instance  
  22. bool HelloWorld::init()  
  23. {  
  24.     //  
  25.     // 1. super init first  
  26.     if ( !Layer::init() )  
  27.     {  
  28.         return false;  
  29.     }  
  30.       
  31.     Size visibleSize = Director::getInstance()->getVisibleSize();  
  32.     Vec2 origin = Director::getInstance()->getVisibleOrigin();  
  33.   
  34.     /  
  35.     // 2. add a menu item with "X" image, which is clicked to quit the program  
  36.     //    you may modify it.  
  37.   
  38.     // add a "close" icon to exit the progress. it's an autorelease object  
  39.     auto closeItem = MenuItemImage::create(  
  40.                                            "CloseNormal.png",  
  41.                                            "CloseSelected.png",  
  42.                                            CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));  
  43.       
  44.     closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,  
  45.                                 origin.y + closeItem->getContentSize().height/2));  
  46.   
  47.     auto adItem = MenuItemImage::create(  
  48.         "CloseNormal.png",  
  49.         "CloseSelected.png",  
  50.         CC_CALLBACK_1(HelloWorld::adCallback, this));  
  51.   
  52.     adItem->setPosition(Vec2(origin.x + visibleSize.width/2 ,origin.y + visibleSize.height/2));  
  53.   
  54.     // create menu, it's an autorelease object  
  55.     auto menu = Menu::create(closeItem,adItem, NULL);  
  56.     menu->setPosition(Vec2::ZERO);  
  57.     this->addChild(menu, 1);  
  58.   
  59.     /  
  60.     // 3. add your codes below...  
  61.   
  62.     // add a label shows "Hello World"  
  63.     // create and initialize a label  
  64.       
  65.     auto label = LabelTTF::create("Hello World""Arial", 24);  
  66.       
  67.     // position the label on the center of the screen  
  68.     label->setPosition(Vec2(origin.x + visibleSize.width/2,  
  69.                             origin.y + visibleSize.height - label->getContentSize().height));  
  70.   
  71.     // add the label as a child to this layer  
  72.     this->addChild(label, 1);  
  73.   
  74.     // add "HelloWorld" splash screen"  
  75.     auto sprite = Sprite::create("HelloWorld.png");  
  76.   
  77.     // position the sprite on the center of the screen  
  78.     sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));  
  79.   
  80.     // add the sprite as a child to this layer  
  81.     this->addChild(sprite, 0);  
  82.       
  83.     return true;  
  84. }  
  85.   
  86.   
  87. void HelloWorld::menuCloseCallback(Ref* pSender)  
  88. {  
  89. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)  
  90.     MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");  
  91.     return;  
  92. #endif  
  93.   
  94.     Director::getInstance()->end();  
  95.   
  96. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  97.     exit(0);  
  98. #endif  
  99. }  
  100.   
  101. void HelloWorld::adCallback(cocos2d::Ref* pRef){  
  102.     Admob::showAd();  
  103. }  

OK,点击最中央的按钮,


转自:http://blog.csdn.net/wiyun_beijing/article/details/17912471

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值