今天尝试了一下 有米、芒果 平台的广告植入,觉得和之前植入百度平台广告的方式不太一样。回想自己在这上面也算是花费了不少时间,现在就把植入广告的过程记录下来,算是对植入广告方式的总结。
1.首先要将应用上传到百度开发者平台,因为后面要用到 API KEY 和 APP ID。
2.下载SDK包(需要集成什么服务就勾选什么),这里我勾选了 广告联盟 和 自动更新
解释下,之前没有集成自动更新功能,结果提交之后没能审核通过。。。。所以,就把自动更新也下载下来了。
【阅读开发手册、查看demo】
因为百度只提供了AndroID的demo,所以使用cocos2d-x植入广告还是挺麻烦的。正因为这样,一定要好好阅读开发手册和demo。好了,不废话了,开工!!!
【步骤1 导入相关demo】
1自动更新&Appx (之后会通过这个demo来完成自己的广告植入代码)
2 BDIntegrationSDK_libProject 这个是将来要引用的
导入之后,将Islibrary 项目 打勾
3.为自己的工程引入BDIntegrationSDK_libProject
具体的方式可参考 百度提供的开发手册,篇幅有限,这里就不一一上图了。
【步骤2 设置APPID 和 APPKEY 以及其他配置】
集成不同的功能需要获得不同的权限,这里,我只集成了自动更新&Appx ,在自己项目的AndroIDManifest.xml中做如下配置
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.richard.southChinaSeaWar" <!-- 我的包名 --> androID:versionCode="2" androID:versionname="2.0" androID:installLocation="auto"> <uses-sdk androID:minSdkVersion="9"/> <uses-feature androID:glEsversion="0x00020000" /> <uses-permission androID:name="androID.permission.INTERNET" /> <uses-permission androID:name="androID.permission.WRITE_SETTINGS" /> <uses-permission androID:name="androID.permission.READ_PHONE_STATE" /> <uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" /> <uses-permission androID:name="androID.permission.ACCESS_WIFI_STATE" /> <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission androID:name="androID.permission.GET_TASKS"/> <uses-permission androID:name="androID.permission.CHANGE_WIFI_STATE"/> <application androID:label="@string/app_name" androID:icon="@drawable/icon"> <!-- Tell Cocos2dxActivity the name of our .so --> <Meta-data androID:name="androID.app.lib_name" androID:value="cocos2dcpp" /> <!-- BDAPPID 和 BDAPPKEY 一定要换成自己的 --> <Meta-data androID:name="BDAPPID" androID:value="7071438"/> <Meta-data androID:name="BDAPPKEY" androID:value="IcdaOe74YsFWW7wGpau4C6dbb7Zya7nT"/> <activity androID:name="org.cocos2dx.cpp.AppActivity" androID:label="@string/app_name" androID:screenorIEntation="portrait" androID:theme="@androID:style/theme.NoTitlebar.Fullscreen" androID:configChanges="orIEntation"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 移动云Appx SDK --> <activity androID:name="com.baIDu.appx.ui.BDInnerbrowser" /> <receiver androID:name="com.baIDu.appx.app_download.CompleteReceiver" > <intent-filter> <action androID:name="androID.intent.action.DOWNLOAD_COMPLETE" /> <action androID:name="androID.intent.action.DOWNLOAD_NOTIFICATION_CliCKED" /> </intent-filter> <intent-filter> <action androID:name="androID.intent.action.PACKAGE_ADDED" /> <data androID:scheme="package" /> <!-- 注意!! 这句必须要加,否则接收不到安装完成广播 --> </intent-filter> </receiver> <!-- 自动更新SDK --> <activity androID:name="com.baIDu.autoupdatesdk.ConfirmDialoigActivity" androID:screenorIEntation="sensor" androID:exported="false" androID:theme="@style/bdp_update_dialog_style_fullscreen" /> <receiver androID:name="com.baIDu.autoupdatesdk.receiver.BDbroadcastReceiver" androID:exported="false"> <intent-filter> <action androID:name="com.baIDu.autoupdatesdk.ACTION_NEW_UPDATE"/> <action androID:name="com.baIDu.autoupdatesdk.ACTION_DOWNLOAD_COMPLETE"/> <action androID:name="com.baIDu.autoupdatesdk.ACTION_NEW_AS"/> <action androID:name="com.baIDu.autoupdatesdk.ACTION_AS_DOWNLOAD_COMPLETE"/> </intent-filter> </receiver> </application> <supports-screens androID:anyDensity="true" androID:smallScreens="true" androID:normalScreens="true" androID:largeScreens="true" androID:xlargeScreens="true"/> <uses-permission androID:name="androID.permission.INTERNET"/> <!-- <uses-permission androID:name="androID.permission.ACCESS_COARSE_LOCATION" /> --> <!-- <uses-permission androID:name="androID.permission.MOUNT_UNMOUNT_fileSYstemS" /> --></manifest>
<!-- 自动更新SDK --> <activity androID:name="com.baIDu.autoupdatesdk.ConfirmDialoigActivity" androID:screenorIEntation="sensor" androID:exported="false" androID:theme="@style/<u>bdp_update_dialog_style_fullscreen</u>" />
没错!就是加了下划线的部分。在styles.xml里面加了以下内容之后OK了
<style name="bdp_update_dialog_style" parent="androID:theme.Dialog"> <item name="androID:windowFrame">@null</item> <item name="androID:windowIsfloating">true</item> <item name="androID:windowIsTranslucent">true</item> <item name="androID:windowNoTitle">true</item><!--除去Title--> <item name="androID:windowContentOverlay">@null</item> <item name="androID:backgroundDimEnabled">true</item> <item name="androID:windowAnimationStyle">@androID:style/Animation.Translucent</item> <item name="androID:windowBackground">@androID:color/transparent</item> </style> <style name="bdp_update_dialog_style_fullscreen" parent="@style/bdp_update_dialog_style"> <item name="androID:windowFullscreen">true</item> </style>
注意,包名,APPID 和 APPKEY一定要设成自己的
【步骤3 引入相关jar,并完成java代码】
1.把BDIntegrationSDK_libProject目录下的libs文件夹中的4个jar文件引入自己项目的libs文件夹中(直接拉过来就好了)
2.接下来完成java部分的代码
当前自己项目的src目录下只有一个AppActivity.java文件,并且里面没什么内容。。。好了,我们再添加一个文件,姑且命名为ShowAdHelper.java,这两个文件是我参考自动更新&Appx 这个demo写的,比较乱,不过能用(*^__^*)
下面就是这两个文件的内容
AppActivity.java
/****************************************************************************copyright (c) 2008-2010 Ricardo Quesadacopyright (c) 2010-2012 cocos2d-x.orgcopyright (c) 2011 Zynga Inc.copyright (c) 2013-2014 Chukong TechnologIEs Inc. http://www.cocos2d-x.orgPermission is hereby granted,free of charge,to any person obtaining a copyof this software and associated documentation files (the "Software"),to dealin the Software without restriction,including without limitation the rightsto use,copy,modify,merge,publish,distribute,sublicense,and/or sellcopIEs of the Software,and to permit persons to whom the Software isfurnished to do so,subject to the following conditions:The above copyright notice and this permission notice shall be included inall copIEs or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS ORIMPLIED,INCLUDING BUT NOT liMITED TO THE WARRANTIES OF MERCHANTABIliTY,fitness FOR A PARTIculaR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR copYRIGHT HolDERS BE liABLE FOR ANY CLaim,damAGES OR OTHERliABIliTY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEAliNGS INTHE SOFTWARE.****************************************************************************/package org.cocos2dx.cpp;import org.cocos2dx.lib.Cocos2dxActivity;import androID.app.ProgressDialog;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.util.Log;import androID.Widget.relativeLayout;import com.baIDu.appx.BDBannerAd;import com.baIDu.appx.BDInterstitialAd;import com.baIDu.appx.BDBannerAd.BannerAdListener;import com.baIDu.appx.BDInterstitialAd.InterstitialAdListener;import com.baIDu.autoupdatesdk.BDautoUpdateSDK;import com.baIDu.autoupdatesdk.UICheckUpdateCallback;public class AppActivity extends Cocos2dxActivity { private static String TAG = "Appx_Interstitial"; private static String TAG1 = "Appx_BannerAd"; static BDBannerAd bannerAdVIEw; BDInterstitialAd appxInterstitialAdVIEw; private ProgressDialog dialog; //插屏广告 public BDInterstitialAd getAd1(){ final BDInterstitialAd appxInterstitialAdVIEw1 = new BDInterstitialAd(this,"IcdaOe74YsFWW7wGpau4C6dbb7Zya7nT","4sw7cIpn9nBuwSX19wm8Ti6S"); // 设置插屏广告行为监听器 appxInterstitialAdVIEw1.setAdListener(new InterstitialAdListener() { @OverrIDe public voID onAdvertisementDataDIDLoadFailure() { Log.e(TAG,"load failure"); appxInterstitialAdVIEw=getAd1(); } @OverrIDe public voID onAdvertisementDataDIDLoadSuccess() { Log.e(TAG,"load success"); } @OverrIDe public voID onAdvertisementVIEwDIDClick() { Log.e(TAG,"on click"); } @OverrIDe public voID onAdvertisementVIEwDIDHIDe() { Log.e(TAG,"on hIDe"); } @OverrIDe public voID onAdvertisementVIEwDIDShow() { Log.e(TAG,"on show"); } @OverrIDe public voID onAdvertisementVIEwWillStartNewIntent() { Log.e(TAG,"leave"); } }); // 加载广告 appxInterstitialAdVIEw1.loadAd(); return appxInterstitialAdVIEw1; } public voID getAd2(){ bannerAdVIEw = new BDBannerAd(this,"SvCiMQ1tehVx0hD6n94bTEhZ"); // 设置横幅广告展示尺寸,如不设置,默认为SIZE_FLEXIBLE; bannerAdVIEw.setAdSize(BDBannerAd.SIZE_FLEXIBLE); // 设置横幅广告行为监听器 bannerAdVIEw.setAdListener(new BannerAdListener() { @OverrIDe public voID onAdvertisementDataDIDLoadFailure() { Log.e(TAG1,"load failure"); } @OverrIDe public voID onAdvertisementDataDIDLoadSuccess() { Log.e(TAG1,"load success"); } @OverrIDe public voID onAdvertisementVIEwDIDClick() { Log.e(TAG1,"on click"); } @OverrIDe public voID onAdvertisementVIEwDIDShow() { Log.e(TAG1,"on show"); } @OverrIDe public voID onAdvertisementVIEwWillStartNewIntent() { Log.e(TAG1,"leave app"); } }); relativeLayout appxBannerContainer = new relativeLayout(this); relativeLayout.LayoutParams parentLayputParams = new relativeLayout.LayoutParams( relativeLayout.LayoutParams.MATCH_PARENT,relativeLayout.LayoutParams.WRAP_CONTENT); this.addContentVIEw(bannerAdVIEw,parentLayputParams); } private Handler aHandler = new Handler() { @OverrIDe public voID handleMessage(Message msg) { switch(msg.what) { case 1: //插屏广告 if(appxInterstitialAdVIEw.isLoaded()){ appxInterstitialAdVIEw.showAd(); Log.d("LC","show"); }else{ appxInterstitialAdVIEw.loadAd(); } Log.d("LC","not show"); break; case 2: //banner广告 getAd2(); Log.d("banner","show"); break; case 3: //banner广告删除 if(bannerAdVIEw!=null){ bannerAdVIEw.destroy(); } break; case 4: //自动更新 myUpdate(); break; } } }; public voID myUpdate() { dialog.show(); BDautoUpdateSDK.uiUpdateAction(this,new MyUICheckUpdateCallback()); Log.e("Game Update","myUpdate success"); } private class MyUICheckUpdateCallback implements UICheckUpdateCallback { @OverrIDe public voID onCheckComplete() { dialog.dismiss(); } } @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); appxInterstitialAdVIEw=getAd1(); dialog = new ProgressDialog(this); dialog.setIndeterminate(true); ShowAdHelper.init(aHandler); }}
ShowAdHelper.java
package org.cocos2dx.cpp;import androID.os.Handler;import androID.os.Message;public class ShowAdHelper { private static Handler mHandler; public static voID init(Handler handler) { ShowAdHelper.mHandler = handler; } public static voID showMyAd(int adTag) { Message msg = mHandler.obtainMessage(); msg.what = adTag; msg.sendToTarget(); }}
Java 部分的工作到这里就结束了,百度提供的demo主要是用于AndroID的,所以cocos2d-x的话,就必须多一个文件 ShowAdHelper.java方便C++通过jni调用 AppActivity.java中的内容
【步骤4 C++层的代码】
到这里就比较简单了,建议新建一个类(这里建AdHelp.h 和 AdHelp.cpp),通过一些static函数来调用广告的显示、关闭。
cocos官方文档对 jni 以及C++通过jni调用Java代码都有介绍,有兴趣深入了解的可查看官网文档
AdHelp.h
#ifndef __AdHelp__H__#define __AdHelp__H__#include <cocos2d.h>using namespace cocos2d;#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include "platform/androID/jni/JniHelper.h"#include <jni.h>#endifclass AdHelp{public: AdHelp(); virtual ~AdHelp(); //通过JNI调用JAVA静态函数,实现展示AD static voID showAd(int adTag);};#endif
#include "AdHelp.h"AdHelp::AdHelp(){}AdHelp::~AdHelp(){}voID AdHelp::showAd(int adTag){ //判断当前是否为AndroID平台 JniMethodInfo showAd;#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; bool isHave = JniHelper::getStaticmethodInfo(minfo,"org/cocos2dx/cpp/ShowAdHelper","showMyAd","(I)V"); if (!isHave) { cclog("jni:showAdStatic is null"); } else { //调用此函数 minfo.env->CallStaticVoIDMethod(minfo.classID,minfo.methodID,adTag); }#endif}
然后在合适的地方调用AdHelp::showAd(2)即可显示横屏广告,其他 *** 作类似。
【步骤5 完工,看一下成果】
横屏广告 插屏广告
植入广告之后再次上传
百度手机助手
91助手
总结以上是内存溢出为你收集整理的cocos2d-x 植入广告(一) 百度横屏、插屏广告全部内容,希望文章能够帮你解决cocos2d-x 植入广告(一) 百度横屏、插屏广告所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)