StartAd.ets 3.9 KB

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