如何展示一个半屏广告

如何展示一个半屏广告

HarmonyOS
2024-08-07 09:30:47
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
开心的兔子
import { router, Prompt } from '@kit.ArkUI';
import { advertising, AdComponent } from '@kit.AdsKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG = 'Ads Demo-SplashHalfScreenAdPage';

@Entry
@Component
export struct SplashHalfScreenAdPage {
  private ads: Array<advertising.Advertisement> = [];
  private displayOptions?: advertising.AdDisplayOptions;

  aboutToAppear() {
    hilog.info(0x0000, TAG, '%{public}s', 'SplashHalfScreenAdPage aboutToAppear');
    const params: Record<string, Object> = router.getParams() as Record<string, Object>;
    if (params && params.ads as Array<advertising.Advertisement> &&
      params.displayOptions as advertising.AdDisplayOptions) {
      this.ads = params.ads as Array<advertising.Advertisement>;
      this.displayOptions = params.displayOptions as advertising.AdDisplayOptions;
    } else {
      hilog.info(0x0000, TAG, '%{public}s', 'empty router params');
    }
  }

  build() {
    Column() {
      AdComponent({
        ads: this.ads, displayOptions: this.displayOptions,
        interactionListener: {
          onStatusChanged: (status: string, ad: advertising.Advertisement, data: string) => {
            switch (status) {
              case AdStatus.AD_OPEN:
                hilog.info(0x0000, TAG, '%{public}s', 'onAdOpen');
                Prompt.showToast({
                  message: 'splash ad open',
                  duration: 1000
                });
                break;
              case AdStatus.AD_CLICKED:
                hilog.info(0x0000, TAG, '%{public}s', 'onAdClick');

                Prompt.showToast({
                  message: 'splash ad click',
                  duration: 1000
                });
                break;
              case AdStatus.AD_CLOSED:
                hilog.info(0x0000, TAG, '%{public}s', 'onAdClose');
                Prompt.showToast({
                  message: 'splash ad close',
                  duration: 1000
                });
                router.replaceUrl({
                  url: 'pages/AdsServicePage',
                })
                break;
            }
          }
        }
      })
        .width('100%')
        .height('87%')

      Column({}) {
        Row() {
          Image($r('app.media.video')).width(24).height(24).margin({ right: 8 })
          Text($r('app.string.video')).fontColor('#1A1A1A').fontSize(16).width(32).height(21)
        }.margin({ bottom: 8 })

        Column() {
          Text($r('app.string.copyright')).fontColor('#1A1A1A').fontSize(9).height(15)
        }
      }.margin({ top: 29, left: 16, right: 16 })
    }.width('100%').height('100%')
  }
}

enum AdStatus {
  AD_OPEN = "onAdOpen",

  AD_CLICKED = "onAdClick",

  AD_CLOSED = "onAdClose"
}
分享
微博
QQ
微信
回复
2024-08-07 11:42:41
相关问题
如何展示一个贴片广告
383浏览 • 1回复 待解决
如何展示一个原生广告
378浏览 • 1回复 待解决
如何展示一个插屏广告
522浏览 • 1回复 待解决
如何展示一个激励广告
368浏览 • 1回复 待解决
如何展示一个开屏广告
505浏览 • 1回复 待解决
如何展示一个banner广告
428浏览 • 1回复 待解决
HarmonyOS 如何实现页面?
267浏览 • 1回复 待解决
HarmonyOS 如何实现展开
466浏览 • 1回复 待解决
如何通过路由的方式打开
398浏览 • 1回复 待解决
如何创建一个window?
334浏览 • 1回复 待解决
如何创建一个worker线程
927浏览 • 1回复 待解决