中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何展示一个半屏广告
微信扫码分享
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" }