cocos creator 接入 优量汇 (以前叫广点通):
https://adnet.qq.com/http://xn--4oqq81ac4mc4rhev
本次接入三种广告:
- 横幅广告 (banner)
- 插屏广告
- 激励广告 视频
➤:首先你得注册账号
➤:新建媒体
➤:新建广告位
效果如下:
(本来为了直观上传的 git图,结果 小格局的 csdn 觉得有广告不给展示)只能在截图示意下
源码在文章的最后面
其中应为广告账号没有实名制,所以 横屏广告,会偶尔加载不出来;
我们的项目 目录结构如下:
- Adbanner 横屏广告
- AdInsert 插屏广告
- AdReward 激励广告
- AdvertisingManager 管理广告的类,Cocos 要调用的静态方法写在这里
- AsbAdvertising 广告基类
- IAdvertising 接口
- MyApplicaition
- package com.cocos.game.adManager;
-
- import android.app.Activity;
- import android.graphics.Color;
- import android.util.Log;
- import android.view.ViewGroup;
- import android.widget.RelativeLayout;
- import com.qq.e.ads.banner2.UnifiedBannerADListener;
- import com.qq.e.ads.banner2.UnifiedBannerView;
- import com.qq.e.comm.util.AdError;
-
- public class AdBanner extends AsbAdvertising implements UnifiedBannerADListener{
-
- private UnifiedBannerView ad;//banner 广告
-
- public AdBanner(Activity _activity,String _id){
- super(_activity,_id);
- this.ad = new UnifiedBannerView(activity, this.adID , this);
-
- RelativeLayout adLayout = new RelativeLayout(activity);
- RelativeLayout.LayoutParams bannerParams =new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
- bannerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
-
- this.ad.setBackgroundColor(Color.WHITE);
- adLayout.addView(this.ad,bannerParams);
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
- activity.addContentView(adLayout,layoutParams);
- }
- @Override
- public void onPreload() {
-
- }
- @Override
- public void onShow() {
- if(this.ad != null ){
- this.ad.loadAD();
- }
- }
-
-
- @Override
- public void onNoAD(AdError adError) {
- String meg = String.format("UnifiedBannerADListener onNoAD, code: %d, message: %s",adError.getErrorCode(),adError.getErrorMsg());
- Log.d(TAG,meg);
- }
-
- @Override
- public void onADReceive() {
-
- }
-
- @Override
- public void onADExposure() {
-
- }
-
- @Override
- public void onADClosed() {
-
- }
-
- @Override
- public void onADClicked() {
-
- }
-
- @Override
- public void onADLeftApplication() {
-
- }
- }
- package com.cocos.game.adManager;
-
- import android.app.Activity;
- import android.util.Base64;
- import android.util.Log;
- import com.qq.e.ads.interstitial2.UnifiedInterstitialAD;
- import com.qq.e.ads.interstitial2.UnifiedInterstitialADListener;
- import com.qq.e.comm.util.AdError;
-
- public class AdInsert extends AsbAdvertising implements UnifiedInterstitialADListener {
- private UnifiedInterstitialAD ad;//插屏广告
- public AdInsert(Activity _activity,String _id){
- super(_activity,_id);
- this.ad = new UnifiedInterstitialAD(activity, this.adID, this);
- this.onPreload();
-
- }
- @Override
- public void onPreload() {
- this.ad.loadAD();
- }
-
- @Override
- public void onShow() {
- if(ad != null&& ad.isValid()){
- ad.show();
- }else {
- Log.d(TAG,"广告未加载完成");
- ad.loadAD();
- }
- }
-
-
- @Override
- public void onADReceive() {
-
- }
-
- @Override
- public void onVideoCached() {
-
- }
-
- @Override
- public void onNoAD(AdError adError) {
- String meg = String.format("UnifiedInterstitialADListener onNoAD, code: %d, message: %s",adError.getErrorCode(),adError.getErrorMsg());
- Log.d(TAG,meg);
- }
-
- @Override
- public void onADOpened() {
-
- }
-
- @Override
- public void onADExposure() {
-
- }
-
- @Override
- public void onADClicked() {
-
- }
-
- @Override
- public void onADLeftApplication() {
-
- }
-
- @Override
- public void onADClosed() {
- this.onPreload();
- String res = "{\"success\":true,\"data\":[{\"id\":\"1524200775692066818\",\"rlsPlanId\":\"0\",\"storeId\":\"1454016017163370498\",\"companyId\":null,\"image\":\"https://img.vandream.com/78/062808d061a8d6.png\",\"imageName\":\"骄阳焊工\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":34,\"companyName\":\"河北骄阳焊工有限公司\",\"companyCode\":\"GF000095885\",\"companyBrand\":\"骄阳焊工\",\"companyLogo\":\"\"},{\"id\":\"1524202782523924481\",\"rlsPlanId\":\"0\",\"storeId\":\"1291662577079709697\",\"companyId\":null,\"image\":\"https://img.vandream.com/78/062814a0a13b9f.png\",\"imageName\":\"矿源\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":33,\"companyName\":\"河南省矿山起重机有限公司\",\"companyCode\":\"GF000001694\",\"companyBrand\":\"矿源\",\"companyLogo\":\"\"},{\"id\":\"1524202654509572098\",\"rlsPlanId\":\"0\",\"storeId\":\"1427882190145855489\",\"companyId\":null,\"image\":\"https://img.vandream.com/77/062813f78460ae.png\",\"imageName\":\"唐山松下\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":32,\"companyName\":\"唐山松下产业机器有限公司\",\"companyCode\":\"GF000000107\",\"companyBrand\":\"松下\",\"companyLogo\":\"\"},{\"id\":\"1524202525794832385\",\"rlsPlanId\":\"0\",\"storeId\":\"1258969183668936705\",\"companyId\":null,\"image\":\"https://img.vandream.com/77/0628117a5b45f7.png\",\"imageName\":\"春雨\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":31,\"companyName\":\"上海友钜五金制品有限公司\",\"companyCode\":\"GF000001726\",\"companyBrand\":\"春雨\",\"companyLogo\":\"https://img.vandream.com/2/2257babff7a7.jpg\"},{\"id\":\"1524202397105135617\",\"rlsPlanId\":\"0\",\"storeId\":\"1330762300927266817\",\"companyId\":null,\"image\":\"https://img.vandream.com/78/062810176c764e.png\",\"imageName\":\"海虹老人\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":30,\"companyName\":\"海虹老人北亚投资有限公司\",\"companyCode\":\"GF000037424\",\"companyBrand\":\"海虹老人HEMPEL\",\"companyLogo\":\"\"},{\"id\":\"1524202245137113090\",\"rlsPlanId\":\"0\",\"storeId\":\"1257203762842411010\",\"companyId\":null,\"image\":\"https://img.vandream.com/78/06280f75c38406.png\",\"imageName\":\"荣利\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":29,\"companyName\":\"杭州浩合螺栓有限公司\",\"companyCode\":\"GF000025106\",\"companyBrand\":\"浩合\",\"companyLogo\":\"https://img.vandream.com/3/43b51c7373cd.png\"}],\"code\":200,\"message\":\"OK\",\"error\":null}";
- AdvertisingManager.callJSFunction(res);
- }
-
- @Override
- public void onRenderSuccess() {
-
- }
-
- @Override
- public void onRenderFail() {
-
- }
- }
- package com.cocos.game.adManager;
-
- import android.app.Activity;
- import android.util.Base64;
- import android.util.Log;
- import com.qq.e.ads.rewardvideo.RewardVideoAD;
- import com.qq.e.ads.rewardvideo.RewardVideoADListener;
- import com.qq.e.comm.util.AdError;
-
-
-
- import java.util.Map;
-
- public class AdReward extends AsbAdvertising implements RewardVideoADListener {
- private RewardVideoAD ad;//激励广告
- private boolean loadFinish;
- public AdReward(Activity _activity, String _id){
- super(_activity, _id);
- this.ad = new RewardVideoAD(activity,this.adID,this,false);
- this.onPreload();
- }
- @Override
- public void onPreload() {
- this.loadFinish = false;
- this.ad.loadAD();
- }
-
- @Override
- public void onShow() {
- if(this.loadFinish && this.ad != null&& this.ad.isValid()){
- this.ad.showAD();
- }else {
- this.ad.loadAD();
- Log.d(TAG,"广告未加载完成");
- }
- }
-
- listener
- @Override
- public void onADLoad() {
- this.loadFinish = true;
- }
-
- @Override
- public void onVideoCached() {
-
- }
-
- @Override
- public void onADShow() {
-
- }
-
- @Override
- public void onADExpose() {
-
- }
-
- @Override
- public void onReward(Map<String, Object> map) {
-
- }
-
- @Override
- public void onADClick() {
-
- }
-
- @Override
- public void onVideoComplete() {
- String res = "{\"success\":true,\"data\":[{\"id\":\"1524200775692066818\",\"rlsPlanId\":\"0\",\"storeId\":\"1454016017163370498\",\"companyId\":null,\"image\":\"https://img.vandream.com/78/062808d061a8d6.png\",\"imageName\":\"骄阳焊工\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":34,\"companyName\":\"河北骄阳焊工有限公司\",\"companyCode\":\"GF000095885\",\"companyBrand\":\"骄阳焊工\",\"companyLogo\":\"\"},{\"id\":\"1524202782523924481\",\"rlsPlanId\":\"0\",\"storeId\":\"1291662577079709697\",\"companyId\":null,\"image\":\"https://img.vandream.com/78/062814a0a13b9f.png\",\"imageName\":\"矿源\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":33,\"companyName\":\"河南省矿山起重机有限公司\",\"companyCode\":\"GF000001694\",\"companyBrand\":\"矿源\",\"companyLogo\":\"\"},{\"id\":\"1524202654509572098\",\"rlsPlanId\":\"0\",\"storeId\":\"1427882190145855489\",\"companyId\":null,\"image\":\"https://img.vandream.com/77/062813f78460ae.png\",\"imageName\":\"唐山松下\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":32,\"companyName\":\"唐山松下产业机器有限公司\",\"companyCode\":\"GF000000107\",\"companyBrand\":\"松下\",\"companyLogo\":\"\"},{\"id\":\"1524202525794832385\",\"rlsPlanId\":\"0\",\"storeId\":\"1258969183668936705\",\"companyId\":null,\"image\":\"https://img.vandream.com/77/0628117a5b45f7.png\",\"imageName\":\"春雨\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":31,\"companyName\":\"上海友钜五金制品有限公司\",\"companyCode\":\"GF000001726\",\"companyBrand\":\"春雨\",\"companyLogo\":\"https://img.vandream.com/2/2257babff7a7.jpg\"},{\"id\":\"1524202397105135617\",\"rlsPlanId\":\"0\",\"storeId\":\"1330762300927266817\",\"companyId\":null,\"image\":\"https://img.vandream.com/78/062810176c764e.png\",\"imageName\":\"海虹老人\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":30,\"companyName\":\"海虹老人北亚投资有限公司\",\"companyCode\":\"GF000037424\",\"companyBrand\":\"海虹老人HEMPEL\",\"companyLogo\":\"\"},{\"id\":\"1524202245137113090\",\"rlsPlanId\":\"0\",\"storeId\":\"1257203762842411010\",\"companyId\":null,\"image\":\"https://img.vandream.com/78/06280f75c38406.png\",\"imageName\":\"荣利\",\"moduleId\":\"1303633342033321986\",\"enabledState\":0,\"state\":null,\"startTime\":null,\"endTime\":null,\"sort\":29,\"companyName\":\"杭州浩合螺栓有限公司\",\"companyCode\":\"GF000025106\",\"companyBrand\":\"浩合\",\"companyLogo\":\"https://img.vandream.com/3/43b51c7373cd.png\"}],\"code\":200,\"message\":\"OK\",\"error\":null}";
- // String strBase64 = Base64.encodeToString(res.getBytes(), Base64.DEFAULT);
- AdvertisingManager.callJSFunction(res);
- }
-
- @Override
- public void onADClose() {
- this.loadFinish = false;
- this.ad.loadAD();
- }
-
- @Override
- public void onError(AdError adError) {
- String meg = String.format("RewardVideoADListener onNoAD, code: %d, message: %s",adError.getErrorCode(),adError.getErrorMsg());
- Log.d(TAG,meg);
- }
-
- }
- package com.cocos.game.adManager;
-
- import android.app.Activity;
- import android.util.Base64;
- import android.util.Log;
-
- import com.cocos.lib.CocosHelper;
- import com.cocos.lib.CocosJavascriptJavaBridge;
- import com.cocos.lib.JsbBridge;
-
- public class AdvertisingManager {
- public static Activity activity;
- public static IAdvertising adInsert;
- public static IAdvertising adBanner;
- public static IAdvertising adReward;
- public AdvertisingManager(Activity _activity) {
- this.activity = _activity;
- this.adInsert = new AdInsert(_activity,"2091645780016154");
- this.adBanner = new AdBanner(_activity,"4080052898050840");
- this.adReward = new AdReward(_activity,"9011264358826997");
- }
-
- public static void showAdInsert(){
- AdvertisingManager.activity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- AdvertisingManager.adInsert.onShow();
- }
- });
- }
- public static void showAdBanner(){
- AdvertisingManager.activity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- AdvertisingManager.adBanner.onShow();
- }
- });
- }
- public static void showAdReward(){
- AdvertisingManager.activity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- AdvertisingManager.adReward.onShow();
- }
- });
- }
-
- static void callJSFunction(final String value){
- String strBase64 = Base64.encodeToString(value.getBytes(), Base64.DEFAULT);
- final String jsCall = String.format("cc.find('Canvas/Layout').getComponent('NativeMannager').showAnimation('%s');", strBase64);
- Log.d("AdvertisingManager",jsCall);
- CocosHelper.runOnGameThread(new Runnable() {
- @Override
- public void run() {
- CocosJavascriptJavaBridge.evalString(jsCall);
- //JsbBridge.sendToScript();
- }
- });
- }
- }
- package com.cocos.game.adManager;
-
- import android.app.Activity;
-
- abstract class AsbAdvertising implements IAdvertising{
- Activity activity;
- String adID;//广告位ID
- String TAG;
- AsbAdvertising(Activity _activity, String id){
- this.activity = _activity;
- this.adID = id;
- this.TAG = this.getClass().getSimpleName();
- }
- }
- package com.cocos.game.adManager;
-
- interface IAdvertising {
- void onPreload();
- void onShow();
- }
- package com.cocos.game;
-
- import android.app.Application;
-
- import com.qq.e.comm.managers.GDTAdSdk;
-
- public class MyApplication extends Application {
- @Override
- public void onCreate() {
- super.onCreate();
- GDTAdSdk.init(this, "1200751593");
- }
- }
只有一句话:
this.advertisingManager = new AdvertisingManager(this);
修改 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.john.nativeTest" android:installLocation="auto"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <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" /> <!-- 可选,如果需要精确定位的话请加上此权限 --> <application android:allowBackup="true" android:name="com.cocos.game.MyApplication" android:label="@string/app_name" android:usesCleartextTraffic="true" tools:replace="android:allowBackup" android:icon="@mipmap/ic_launcher"> <!-- Tell CocosNativeActivity the name of our .so --> <meta-data android:name="android.app.lib_name" android:value="cocos" /> <activity android:name="com.cocos.game.AppActivity" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden|screenSize|screenLayout" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:launchMode="singleTask" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.cocos.lib.CocosEditBoxActivity" android:configChanges="orientation|keyboardHidden|screenSize|screenLayout" android:screenOrientation="behind" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" /> <activity android:name="com.qq.e.ads.PortraitADActivity" tools:replace="android:screenOrientation" android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation|screenSize" /> <activity android:name="com.qq.e.ads.LandscapeADActivity" tools:replace="android:screenOrientation" android:screenOrientation="landscape" android:configChanges="keyboard|keyboardHidden|orientation|screenSize" /> <provider android:name="com.qq.e.comm.GDTFileProvider" android:authorities="${applicationId}.gdt.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/gdt_file_path" /> </provider> </application> </manifest>没有 libs 文件夹的话,新建 libs 文件夹,SDK 拖进去
.gradle 添加一句话:
repositories { flatDir { dirs 'libs' } }
然后就是 Cocos Creator 调用代码了:
源码下载地址: