插屏广告是 app 运行时,以弹窗显示的一种广告形式。
 1.通过AMPSInterstitialAd的showAd方法弹窗显示广告
 2.通过在布局中使用AMPSBuildInterstitialView直接加载
        | 属性 | 说明 | 必须 | 
|---|---|---|
| spaceId | 广告位 ID | 是 | 
| apiKey | 商户 ID | 否 | 
| timeoutInterval | 拉取广告超时时间 | 否 | 
| 方法 | 说明 | 
|---|---|
| constructor(option: ampsAd.AdOptions, callBack: ampsAd.CallBack) | 构造方法创建广告对象,option 参数配置,callBack:回调方法集 | 
| loadAd | 广告请求 | 
| showAd | 通过弹窗形式展示广告,参数传当前组件 this | 
| getECPM | 获取竞价 | 
| notifyRTBWin | 竞价成功上报 | 
| notifyRTBLoss | 竞价失败上报 | 
AMPSBuildInterstitialView 是通过组件形式直接在布局中显示方式。结合Stack层叠布局达到弹窗效果。
1.通过AMPSBuildInterstitialView开发者在布局中利用Stack自行根据回调显示和关闭控制。
  build() {
    Stack() {
      Scroll() {
        Column({ space: 10 }) {
          Text("页面显示内容")
        }
         
      }
      .height('100%')
      .padding({ top: 50 })
      //通过AMPSBuildInterstitialView显示插屏内容
      if (this.showFlag) {
        AMPSBuildInterstitialView(this.interAd)
      }
    }
  }
        2.在回调方法 onRenderOk 中 设置showFlag 为 true,刷新页面显示广告。在关闭回调中监听关闭,通过showFlag状态移除广告组件。
 onRenderOk: (): void => { 
  this.showFlag = true
},
onAdClosed: (): void => {
  this.showFlag = false
  }
        | 方法 | 说明 | 
|---|---|
| onLoadSuccess?: () => void | 插屏广告加载成功 | 
| onLoadFailure?: (code: number, message: string) => void | 插屏广告加载失败 | 
| onRenderOk?: () => void | 插屏广告渲染数据准备成功 | 
| onRenderFailure?: () => void | 插屏广告渲染数据准备失败 | 
| onAdShown?: () => void | 插屏广告显示 | 
| onAdExposure?: () => void | 插屏广告曝光 | 
| onAdClicked?: () => void | 插屏广告点击 | 
| onAdClosed?: () => void | 插屏广告关闭 | 
| onVideoPlayStart?: () => void | 插屏素材视频播放开始 | 
| onVideoPlayEnd?: () => void | 插屏视频素材播放结束 | 
| onVideoPlayError?: () => void | 插屏视频素材播放异常 | 
| onVideoSkipToEnd?: () => void | 插屏视频素材跳到结束 | 
广告加载与显示:
@Entry
@Component
struct InterstitialPage {
  spaceId: string = TextIds.AMPS_SPACE_ID_INTERSTITIAL
  @State showAd: boolean = false
  @State isLoading: boolean = false
  pathStack: NavPathStack = new NavPathStack()
  callback: ampsAd.CallBack = {
    onLoadSuccess: (): void => {
      console.log('-----InterstitialPage-onLoadSuccess------')
    },
    onLoadFailure: (code: number, message: string): void => {
      console.log('-----InterstitialPage-onLoadFailure:'+ message)
      promptAction.showToast({ message: `${message}:code=${code}` })
    },
    onRenderOk: (): void => {
      console.log('-----InterstitialPage-onRenderOk------')
      //方式一
      if (this.interAd.hasSHowAdMethod()) {
        this.interAd.showAd({ windowStage: EntryAbility.windowStage,uiContext:this.getUIContext() })
      } else {
        //方式二
        this.showAd = true
      }
    },
    onAdShow: (): void => {
      console.log('-----InterstitialPage-onAdShow------')
    },
    onAdExposure: (): void => {
      console.log('-----InterstitialPage-onAdExposure------')
    },
    onAdClicked: (): void => {
      console.log('-----InterstitialPage-onAdClicked------')
    },
    onAdClosed: (): void => {
      this.showAd = false
      console.log('-----InterstitialPage-onAdClosed------')
    },
    onVideoPlayStart(){
      console.log('-----InterstitialPage-onVideoPlayStart------')
    },
    onVideoPlayEnd(){
      console.log('-----InterstitialPage-onVideoPlayEnd------')
    },
    onVideoPlayError(){
      console.log('-----InterstitialPage-onVideoPlayError------')
    },
    onVideoSkipToEnd(){
      console.log('-----InterstitialPage-onVideoSkipToEnd------')
    }
  }
  interAd: AMPSInterstitialAd = new AMPSInterstitialAd({
    spaceId: this.spaceId,
  }, this.callback)
  requestInterAd() {
    this.interAd = new AMPSInterstitialAd({
      spaceId: this.spaceId,
    }, this.callback)
    this.interAd.load()
  }
  build() {
    Stack() {
      Column() {
        Row() {
          Button("返回")
            .fontColor(Color.Black)
            .fontSize(22)
            .align(Alignment.BottomStart)
            .backgroundColor(Color.Transparent)
            .onClick(() => {
              router.back()
            })
        }
        .alignItems(VerticalAlign.Bottom)
        .width('100%')
        .height("60")
        Column({ space: 10 }) {
          Row({ space: 5 }) {
            Text("spaceId:")
            TextInput({ placeholder: '请输入spaceId', text: $$this.spaceId }).layoutWeight(1)
          }
          .width('80%')
          if (this.isLoading) {
            Text("广告加载中。。。")
          } else {
            Button('加载插屏广告')
              .onClick(() => {
                this.requestInterAd()
              })
          }
        }
        .justifyContent(FlexAlign.Center)
        .layoutWeight(1)
        .width('100%')
      }
      if (this.showAd) {
        Column() {
          Stack() {
            AMPSBuildInterstitialView(this.interAd)
          }
          .flexGrow(1)
          .width('100%')
          .height("100%")
        }
        .width("100%")
        .height("100%")
      }
    }
  }
}