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

2023-11-22 22:40

本文主要是介绍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

转载于:https://my.oschina.net/fgreshrht/blog/378931

这篇关于cocos2d-x 3.X 在android 中添加多盟插屏广告的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/413440

相关文章

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I