StartAd.ets 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { common } from '@kit.AbilityKit';
  2. import { advertising } from '@kit.AdsKit';
  3. import { router } from '@kit.ArkUI';
  4. import { hilog } from '@kit.PerformanceAnalysisKit';
  5. import { emitter } from '@kit.BasicServicesKit';
  6. import { YTLog } from '../../../../Index';
  7. import { AppStorageKeyCollect } from '../constants';
  8. export enum AdType {
  9. // 开屏广告的类型
  10. SPLASH_AD = 1
  11. }
  12. export class JhStartAd {
  13. // 鲸鸿开屏广告的单例
  14. private static declare instance: JhStartAd
  15. private declare context: common.UIAbilityContext
  16. private oaid: string = '';
  17. private isTimeOut: boolean = false;
  18. // 超时时间(单位毫秒),开发者可根据实际情况修改
  19. private timeOutDuration: number = 1 * 1000;
  20. // 超时index
  21. private timeOutIndex: number = -1;
  22. // 广告展示参数
  23. private declare ads: Array<advertising.Advertisement>
  24. private adDisplayOptions: advertising.AdDisplayOptions = {
  25. // 是否静音,默认不静音
  26. mute: false
  27. }
  28. // 广告配置
  29. private adOptions: advertising.AdOptions = {
  30. // 是否允许流量下载0:不允许,1:允许,不设置以广告主设置为准
  31. allowMobileTraffic: 0,
  32. // 是否希望根据 COPPA 的规定将您的内容视为面向儿童的内容: -1默认值,不确定 0不希望 1希望
  33. tagForChildProtection: -1,
  34. // 是否希望按适合未达到法定承诺年龄的欧洲经济区 (EEA) 用户的方式处理该广告请求: -1默认值,不确定 0不希望 1希望
  35. tagForUnderAgeOfPromise: -1,
  36. // 设置广告内容分级上限: W: 3+,所有受众 PI: 7+,家长指导 J:12+,青少年 A: 16+/18+,成人受众
  37. adContentClassification: 'A'
  38. }
  39. // 开屏视频广告请求参数
  40. private splashVideoAdReqParams: advertising.AdRequestParams = {
  41. // 'testd7c5cewoj6'为测试专用的广告位ID,App正式发布时需要改为正式的广告位ID
  42. adId: 'testd7c5cewoj6',
  43. adType: AdType.SPLASH_AD,
  44. adCount: 1,
  45. oaid: this.oaid
  46. }
  47. // 开屏图片广告请求参数
  48. private splashImageAdReqParams: advertising.AdRequestParams = {
  49. // 'testq6zq98hecj'为测试专用的广告位ID,App正式发布时需要改为正式的广告位ID
  50. adId: 'testq6zq98hecj',
  51. adType: AdType.SPLASH_AD,
  52. adCount: 1,
  53. oaid: this.oaid
  54. }
  55. //进一步优化单例 使外部无法创建实例
  56. private constructor() {
  57. }
  58. //获取单例
  59. static getInstance() {
  60. if (!JhStartAd.instance) {
  61. JhStartAd.instance = new JhStartAd()
  62. }
  63. return JhStartAd.instance
  64. }
  65. init(UIContext: common.UIAbilityContext, oaid: string) {
  66. this.context = UIContext
  67. this.oaid = oaid
  68. this.requestAd(this.splashImageAdReqParams, this.adOptions)
  69. }
  70. private requestAd(adReqParams: advertising.AdRequestParams, adOptions: advertising.AdOptions): void {
  71. // 广告请求回调监听
  72. const adLoaderListener: advertising.AdLoadListener = {
  73. // 广告请求失败回调
  74. onAdLoadFailure: (errorCode: number, errorMsg: string) => {
  75. clearTimeout(this.timeOutIndex);
  76. YTLog.error(errorCode + ':' + errorMsg)
  77. if (this.isTimeOut) {
  78. return;
  79. }
  80. emitter.emit('adLoadFail')
  81. },
  82. // 广告请求成功回调
  83. onAdLoadSuccess: (ads: Array<advertising.Advertisement>) => {
  84. clearTimeout(this.timeOutIndex);
  85. if (this.isTimeOut) {
  86. return;
  87. }
  88. YTLog.info('广告加载成功')
  89. if (canIUse("SystemCapability.Advertising.Ads")) {
  90. this.ads = ads
  91. AppStorage.setOrCreate(AppStorageKeyCollect.AD_IS_LOADED, true)
  92. }
  93. }
  94. };
  95. // 创建AdLoader广告对象
  96. const load: advertising.AdLoader = new advertising.AdLoader(this.context);
  97. // 调用广告请求接口
  98. this.timeOutHandler();
  99. load.loadAd(adReqParams, adOptions, adLoaderListener);
  100. }
  101. private timeOutHandler(): void {
  102. this.isTimeOut = false;
  103. // 超时处理
  104. this.timeOutIndex = setTimeout(() => {
  105. this.isTimeOut = true;
  106. const options: router.RouterOptions = {
  107. // 开发者可根据项目实际情况修改超时之后要跳转的目标页面
  108. url: 'pages/Index',
  109. };
  110. router.pushUrl(options);
  111. hilog.error(0x0000, 'testTag', '%{public}s', 'load ad time out');
  112. }, this.timeOutDuration);
  113. }
  114. }
  115. export const jHStartAd = JhStartAd.getInstance()