转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/35986939
DEMO下载地址:http://download.csdn.net/detail/oyangyufu/7578451
在经过国内的几个广告平台尝试,终于选择芒果广告,有几个长处:SDK更新较快,支持cocos2d-x3.0。平台还聚合了国内主流广告平台,下载对应的SDK也可载入到project中。
芒果横幅广告測试
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);
/**
* 初始化全插屏对象
* 初始化之前必须设置默认的AppKey和Activity
*/
AdsMogoInterstitialManager.setDefaultInitAppKey("93535c6092f543e8a257ee435a69da06");
AdsMogoInterstitialManager.setInitActivity(Test.this);
AdsMogoInterstitialManager.shareInstance()
.initDefaultInterstitial();
AdsMogoInterstitialManager.shareInstance()
.defaultInterstitial()
.setAdsMogoInterstitialListener(new AdsMogoInterstitialListener() {
@Override
public void onShowInterstitialScreen(String arg0) {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onShowInterstitialScreen");
}
@Override
public boolean onInterstitialStaleDated(String arg0) {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onInterstitialStaleDated");
return false;
}
@Override
public void onInterstitialRealClickAd(String arg0) {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onInterstitialRealClickAd");
}
@Override
public View onInterstitialGetView() {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onInterstitialGetView");
return bannerLayout;
}
@Override
public void onInterstitialCloseAd(boolean arg0) {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onInterstitialCloseAd");
}
@Override
public boolean onInterstitialClickCloseButton() {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onInterstitialClickCloseButton");
return false;
}
@Override
public void onInterstitialClickAd(String arg0) {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onInterstitialClickAd");
}
@Override
public Class getCustomEvemtPlatformAdapterClass(
AdsMogoCustomEventPlatformEnum arg0) {
// TODO Auto-generated method stub
return null;
}
});
//
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 0:
// 展示Banner
if (bannerLayout.getChildCount() == 0) {
adView = new AdsMogoLayout(Test.this,
"46184ad32a0c417887cb433203b11403");
adView.setAdsMogoListener(new AdsMogoListener() {
@Override
public void onRequestAd(String arg0) {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onRequestAd");
}
@Override
public void onReceiveAd(ViewGroup arg0, String arg1) {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onReceiveAd");
}
@Override
public void onRealClickAd() {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onRealClickAd");
}
@Override
public void onFailedReceiveAd() {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onFailedReceiveAd");
}
@Override
public void onCloseMogoDialog() {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onCloseMogoDialog");
}
@Override
public boolean onCloseAd() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onClickAd(String arg0) {
// TODO Auto-generated method stub
Log.v("MogoCocos2dx Demo", "onClickAd");
}
@Override
public Class getCustomEvemtPlatformAdapterClass(
AdsMogoCustomEventPlatformEnum arg0) {
// TODO Auto-generated method stub
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:
/**
*进入展示时机
*当应用须要展示全屏广告调用interstitialShow(boolean isWait);
*通知SDK进入展示时机,SDK会竭尽全力展示出广告,当然因为网络等问题不能马上展示
*广告的,您能够通过參数isWait来控制授权SDK在获得到广告后马上展示广告。
*/
AdsMogoInterstitialManager.shareInstance()
.defaultInterstitial().interstitialShow(true);
break;
case 4:
Test.this.finish();
break;
case 5:
/**
*退出展示时机
*假设您之前进入了展示时机,而且isWait參数设置为YES,那么在须要取消等待广告展示的
*时候调用方法interstitialCancel();来通知SDK
*/
AdsMogoInterstitialManager.shareInstance()
.defaultInterstitial().interstitialCancel();
break;
}
}
};
}
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
// Test should create stencil buffer
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、创建菜单button。点击显示广告
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、效果图