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使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(