• Cocos Creator 3.x 优量汇/广点通 android


    cocos creator 接入 优量汇 (以前叫广点通):

    https://adnet.qq.com/http://xn--4oqq81ac4mc4rhev

    本次接入三种广告:

    1. 横幅广告 (banner)
    2. 插屏广告
    3. 激励广告 视频

    ➤:首先你得注册账号

    ➤:新建媒体

    ➤:新建广告位

     

    效果如下:

    (本来为了直观上传的 git图,结果 小格局的 csdn 觉得有广告不给展示)只能在截图示意下

     

     

     

     

    源码在文章的最后面

    其中应为广告账号没有实名制,所以  横屏广告,会偶尔加载不出来;

     我们的项目 目录结构如下:

    • Adbanner  横屏广告
    • AdInsert  插屏广告
    • AdReward 激励广告
    • AdvertisingManager 管理广告的类,Cocos 要调用的静态方法写在这里
    • AsbAdvertising 广告基类
    • IAdvertising 接口
    • MyApplicaition

     Adbanner.java

    1. package com.cocos.game.adManager;
    2. import android.app.Activity;
    3. import android.graphics.Color;
    4. import android.util.Log;
    5. import android.view.ViewGroup;
    6. import android.widget.RelativeLayout;
    7. import com.qq.e.ads.banner2.UnifiedBannerADListener;
    8. import com.qq.e.ads.banner2.UnifiedBannerView;
    9. import com.qq.e.comm.util.AdError;
    10. public class AdBanner extends AsbAdvertising implements UnifiedBannerADListener{
    11. private UnifiedBannerView ad;//banner 广告
    12. public AdBanner(Activity _activity,String _id){
    13. super(_activity,_id);
    14. this.ad = new UnifiedBannerView(activity, this.adID , this);
    15. RelativeLayout adLayout = new RelativeLayout(activity);
    16. RelativeLayout.LayoutParams bannerParams =new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
    17. bannerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    18. this.ad.setBackgroundColor(Color.WHITE);
    19. adLayout.addView(this.ad,bannerParams);
    20. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
    21. activity.addContentView(adLayout,layoutParams);
    22. }
    23. @Override
    24. public void onPreload() {
    25. }
    26. @Override
    27. public void onShow() {
    28. if(this.ad != null ){
    29. this.ad.loadAD();
    30. }
    31. }
    32. @Override
    33. public void onNoAD(AdError adError) {
    34. String meg = String.format("UnifiedBannerADListener onNoAD, code: %d, message: %s",adError.getErrorCode(),adError.getErrorMsg());
    35. Log.d(TAG,meg);
    36. }
    37. @Override
    38. public void onADReceive() {
    39. }
    40. @Override
    41. public void onADExposure() {
    42. }
    43. @Override
    44. public void onADClosed() {
    45. }
    46. @Override
    47. public void onADClicked() {
    48. }
    49. @Override
    50. public void onADLeftApplication() {
    51. }
    52. }

    AdInsert.java

    1. package com.cocos.game.adManager;
    2. import android.app.Activity;
    3. import android.util.Base64;
    4. import android.util.Log;
    5. import com.qq.e.ads.interstitial2.UnifiedInterstitialAD;
    6. import com.qq.e.ads.interstitial2.UnifiedInterstitialADListener;
    7. import com.qq.e.comm.util.AdError;
    8. public class AdInsert extends AsbAdvertising implements UnifiedInterstitialADListener {
    9. private UnifiedInterstitialAD ad;//插屏广告
    10. public AdInsert(Activity _activity,String _id){
    11. super(_activity,_id);
    12. this.ad = new UnifiedInterstitialAD(activity, this.adID, this);
    13. this.onPreload();
    14. }
    15. @Override
    16. public void onPreload() {
    17. this.ad.loadAD();
    18. }
    19. @Override
    20. public void onShow() {
    21. if(ad != null&& ad.isValid()){
    22. ad.show();
    23. }else {
    24. Log.d(TAG,"广告未加载完成");
    25. ad.loadAD();
    26. }
    27. }
    28. @Override
    29. public void onADReceive() {
    30. }
    31. @Override
    32. public void onVideoCached() {
    33. }
    34. @Override
    35. public void onNoAD(AdError adError) {
    36. String meg = String.format("UnifiedInterstitialADListener onNoAD, code: %d, message: %s",adError.getErrorCode(),adError.getErrorMsg());
    37. Log.d(TAG,meg);
    38. }
    39. @Override
    40. public void onADOpened() {
    41. }
    42. @Override
    43. public void onADExposure() {
    44. }
    45. @Override
    46. public void onADClicked() {
    47. }
    48. @Override
    49. public void onADLeftApplication() {
    50. }
    51. @Override
    52. public void onADClosed() {
    53. this.onPreload();
    54. 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}";
    55. AdvertisingManager.callJSFunction(res);
    56. }
    57. @Override
    58. public void onRenderSuccess() {
    59. }
    60. @Override
    61. public void onRenderFail() {
    62. }
    63. }

    AdReward.java

    1. package com.cocos.game.adManager;
    2. import android.app.Activity;
    3. import android.util.Base64;
    4. import android.util.Log;
    5. import com.qq.e.ads.rewardvideo.RewardVideoAD;
    6. import com.qq.e.ads.rewardvideo.RewardVideoADListener;
    7. import com.qq.e.comm.util.AdError;
    8. import java.util.Map;
    9. public class AdReward extends AsbAdvertising implements RewardVideoADListener {
    10. private RewardVideoAD ad;//激励广告
    11. private boolean loadFinish;
    12. public AdReward(Activity _activity, String _id){
    13. super(_activity, _id);
    14. this.ad = new RewardVideoAD(activity,this.adID,this,false);
    15. this.onPreload();
    16. }
    17. @Override
    18. public void onPreload() {
    19. this.loadFinish = false;
    20. this.ad.loadAD();
    21. }
    22. @Override
    23. public void onShow() {
    24. if(this.loadFinish && this.ad != null&& this.ad.isValid()){
    25. this.ad.showAD();
    26. }else {
    27. this.ad.loadAD();
    28. Log.d(TAG,"广告未加载完成");
    29. }
    30. }
    31. listener
    32. @Override
    33. public void onADLoad() {
    34. this.loadFinish = true;
    35. }
    36. @Override
    37. public void onVideoCached() {
    38. }
    39. @Override
    40. public void onADShow() {
    41. }
    42. @Override
    43. public void onADExpose() {
    44. }
    45. @Override
    46. public void onReward(Map<String, Object> map) {
    47. }
    48. @Override
    49. public void onADClick() {
    50. }
    51. @Override
    52. public void onVideoComplete() {
    53. 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}";
    54. // String strBase64 = Base64.encodeToString(res.getBytes(), Base64.DEFAULT);
    55. AdvertisingManager.callJSFunction(res);
    56. }
    57. @Override
    58. public void onADClose() {
    59. this.loadFinish = false;
    60. this.ad.loadAD();
    61. }
    62. @Override
    63. public void onError(AdError adError) {
    64. String meg = String.format("RewardVideoADListener onNoAD, code: %d, message: %s",adError.getErrorCode(),adError.getErrorMsg());
    65. Log.d(TAG,meg);
    66. }
    67. }

    AdvertisingManager.java

    1. package com.cocos.game.adManager;
    2. import android.app.Activity;
    3. import android.util.Base64;
    4. import android.util.Log;
    5. import com.cocos.lib.CocosHelper;
    6. import com.cocos.lib.CocosJavascriptJavaBridge;
    7. import com.cocos.lib.JsbBridge;
    8. public class AdvertisingManager {
    9. public static Activity activity;
    10. public static IAdvertising adInsert;
    11. public static IAdvertising adBanner;
    12. public static IAdvertising adReward;
    13. public AdvertisingManager(Activity _activity) {
    14. this.activity = _activity;
    15. this.adInsert = new AdInsert(_activity,"2091645780016154");
    16. this.adBanner = new AdBanner(_activity,"4080052898050840");
    17. this.adReward = new AdReward(_activity,"9011264358826997");
    18. }
    19. public static void showAdInsert(){
    20. AdvertisingManager.activity.runOnUiThread(new Runnable() {
    21. @Override
    22. public void run() {
    23. AdvertisingManager.adInsert.onShow();
    24. }
    25. });
    26. }
    27. public static void showAdBanner(){
    28. AdvertisingManager.activity.runOnUiThread(new Runnable() {
    29. @Override
    30. public void run() {
    31. AdvertisingManager.adBanner.onShow();
    32. }
    33. });
    34. }
    35. public static void showAdReward(){
    36. AdvertisingManager.activity.runOnUiThread(new Runnable() {
    37. @Override
    38. public void run() {
    39. AdvertisingManager.adReward.onShow();
    40. }
    41. });
    42. }
    43. static void callJSFunction(final String value){
    44. String strBase64 = Base64.encodeToString(value.getBytes(), Base64.DEFAULT);
    45. final String jsCall = String.format("cc.find('Canvas/Layout').getComponent('NativeMannager').showAnimation('%s');", strBase64);
    46. Log.d("AdvertisingManager",jsCall);
    47. CocosHelper.runOnGameThread(new Runnable() {
    48. @Override
    49. public void run() {
    50. CocosJavascriptJavaBridge.evalString(jsCall);
    51. //JsbBridge.sendToScript();
    52. }
    53. });
    54. }
    55. }

    AsbAdvertising.java

    1. package com.cocos.game.adManager;
    2. import android.app.Activity;
    3. abstract class AsbAdvertising implements IAdvertising{
    4. Activity activity;
    5. String adID;//广告位ID
    6. String TAG;
    7. AsbAdvertising(Activity _activity, String id){
    8. this.activity = _activity;
    9. this.adID = id;
    10. this.TAG = this.getClass().getSimpleName();
    11. }
    12. }

    IAdvertising.java

    1. package com.cocos.game.adManager;
    2. interface IAdvertising {
    3. void onPreload();
    4. void onShow();
    5. }

    MyApplication.java

    1. package com.cocos.game;
    2. import android.app.Application;
    3. import com.qq.e.comm.managers.GDTAdSdk;
    4. public class MyApplication extends Application {
    5. @Override
    6. public void onCreate() {
    7. super.onCreate();
    8. GDTAdSdk.init(this, "1200751593");
    9. }
    10. }

    AppActivity 中启动:

    只有一句话:

    this.advertisingManager = new AdvertisingManager(this);

    修改 AndroidManifest.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. package="com.john.nativeTest"
    5. android:installLocation="auto">
    6. <uses-permission android:name="android.permission.INTERNET"/>
    7. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    8. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    9. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    10. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    11. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 可选,如果需要精确定位的话请加上此权限 -->
    12. <application
    13. android:allowBackup="true"
    14. android:name="com.cocos.game.MyApplication"
    15. android:label="@string/app_name"
    16. android:usesCleartextTraffic="true"
    17. tools:replace="android:allowBackup"
    18. android:icon="@mipmap/ic_launcher">
    19. <!-- Tell CocosNativeActivity the name of our .so -->
    20. <meta-data android:name="android.app.lib_name"
    21. android:value="cocos" />
    22. <activity
    23. android:name="com.cocos.game.AppActivity"
    24. android:screenOrientation="portrait"
    25. android:configChanges="orientation|keyboardHidden|screenSize|screenLayout"
    26. android:label="@string/app_name"
    27. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    28. android:launchMode="singleTask"
    29. android:exported="true">
    30. <intent-filter>
    31. <action android:name="android.intent.action.MAIN" />
    32. <category android:name="android.intent.category.LAUNCHER" />
    33. </intent-filter>
    34. </activity>
    35. <activity
    36. android:name="com.cocos.lib.CocosEditBoxActivity"
    37. android:configChanges="orientation|keyboardHidden|screenSize|screenLayout"
    38. android:screenOrientation="behind"
    39. android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
    40. <activity
    41. android:name="com.qq.e.ads.PortraitADActivity"
    42. tools:replace="android:screenOrientation"
    43. android:screenOrientation="portrait"
    44. android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />
    45. <activity
    46. android:name="com.qq.e.ads.LandscapeADActivity"
    47. tools:replace="android:screenOrientation"
    48. android:screenOrientation="landscape"
    49. android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />
    50. <provider
    51. android:name="com.qq.e.comm.GDTFileProvider"
    52. android:authorities="${applicationId}.gdt.fileprovider"
    53. android:exported="false"
    54. android:grantUriPermissions="true">
    55. <meta-data
    56. android:name="android.support.FILE_PROVIDER_PATHS"
    57. android:resource="@xml/gdt_file_path" />
    58. </provider>
    59. </application>
    60. </manifest>

    没有 libs 文件夹的话,新建 libs 文件夹,SDK 拖进去

    .gradle 添加一句话:

    repositories {
        flatDir {
            dirs 'libs'
        }
    }
    

     然后就是 Cocos Creator 调用代码了:

    源码下载地址:

    CocosCreator优量汇/广点通android-Javascript文档类资源-CSDN下载CocosCreator3.x原生TS交互androidCocosCreator3更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/nicepainkiller/86402851

  • 相关阅读:
    java:JDBC ResultSet结合Spring的TransactionTemplate事务模板的查询方式
    Java版工程行业管理系统源码-专业的工程管理软件- 工程项目各模块及其功能点清单
    DRCNN:超越高斯去噪:深度CNN图像去噪的残差学习
    proxysql-安装部署
    java求两个数的百分比
    QT-输入输出
    Javascript前端解析操作返回的各种类型的数据
    Realtek SDK缓冲区溢出和命令执行漏洞说明
    Java校园二手平台项目商城电商购物系统(含源码+论文+答辩PPT等)
    HTML5期末大作业:北京旅游网页设计制作(1页) 简单静态HTML网页作品 我的旅游网页作业成品 学生旅游网站模板
  • 原文地址:https://blog.csdn.net/nicepainkiller/article/details/126380761