设置插页式广告

插页式广告是全屏广告,会覆盖整个应用界面。您可以在应用流程的自然过渡点(例如,活动之间的切换处或游戏关卡之间的暂停时段)展示这些广告。

当应用展示插页式广告时,用户可以点按广告继续操作,也可以关闭广告返回应用。欢迎阅读我们的案例研究

本指南介绍了如何将插页式广告植入到 Android 应用中。

准备工作

在继续之前,请执行以下操作:

  • 设置 GMA Next-Gen SDK
  • 使用测试插页式广告单元 ID ca-app-pub-3940256099942544/1033173712
    • 在构建和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。如果您不使用测试广告单元 ID,Google 可能会暂停您的账号。
    • 在发布应用之前,请将此 ID 替换为您的广告单元 ID。
    • 如需详细了解 GMA Next-Gen SDK 测试广告,请参阅 启用测试广告

了解广告预加载

GMA Next-Gen SDK 中的广告预加载功能可自动执行广告加载和缓存操作。

广告预加载具有以下优势:

  • 引用管理:在广告展示之前维护引用。
  • 自动重新加载:从缓存中检索到广告后,加载新广告。
  • 托管重试:在广告加载失败时加载新广告。
  • 到期处理:在广告到期之前刷新广告。
  • 缓存优化:优化缓存顺序,以投放优先级最高的 广告。

开始广告预加载

如需开始预加载广告,请在应用启动时调用一次 startPreload() 方法。调用 startPreload() 方法后, GMA Next-Gen SDK 会自动预加载广告,并重试 预加载配置的失败请求。

以下示例展示了如何开始预加载广告:

Kotlin

// Call start() once after SDK initialization.
// Preload only one ad unit per format to optimize performance.
val adRequest = AdRequest.Builder(adUnitId).build()
val preloadConfig = PreloadConfiguration(adRequest)
InterstitialAdPreloader.start(adUnitId, preloadConfig)

Java

// Call start() once after SDK initialization.
// Preload only one ad unit per format to optimize performance.
AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest);
InterstitialAdPreloader.start(adUnitId, preloadConfig);

请将 AD_UNIT_ID 替换为您的广告单元 ID。

上一个示例展示了如何将广告单元 ID 用作预加载 ID。预加载 ID 是您创建的字符串标识符,用于标识广告预加载配置。如果您的应用需要针对同一广告单元 ID 使用多个定位配置,请传递自定义字符串标识符。

获取并展示预加载的广告

当您想要展示广告时,请调用 pollAd() 方法。 GMA Next-Gen SDK 会检索可用的广告,并自动预加载下一个广告 在后台。如果没有可用的广告,GMA Next-Gen SDK 不会返回任何广告。

当您有可用的广告对象时,请调用 show 方法来展示广告。 以下示例展示了如何检索和展示预加载的广告:

Kotlin

private fun pollAndShowAd(activity: Activity, adUnitId: String) {
  // Polling returns the next available ad and loads another ad in the background.
  val ad = InterstitialAdPreloader.pollAd(adUnitId)
  if (ad == null) {
    Log.e(TAG, "Interstitial ad is not available.")
    return
  }

  // Interact with the ad object as needed.
  Log.d(TAG, "Interstitial ad response info: ${ad.getResponseInfo()}")
  ad.adEventCallback =
    object : InterstitialAdEventCallback {
      override fun onAdImpression() {
        Log.d(TAG, "Interstitial ad recorded an impression.")
      }
    }
  ad.show(activity)
}

Java

private void pollAndShowAd(Activity activity, String adUnitId) {
  // Polling returns the next available ad and loads another ad in the background.
  final InterstitialAd ad = InterstitialAdPreloader.pollAd(adUnitId);

  // Interact with the ad object as needed.
  if (ad == null) {
    Log.e(TAG, "Interstitial ad is not available.");
    return;
  }

  Log.d(TAG, "Interstitial ad response info: " + ad.getResponseInfo());
  ad.setAdEventCallback(
      new InterstitialAdEventCallback() {
        @Override
        public void onAdImpression() {
          Log.d(TAG, "Interstitial ad recorded an impression.");
        }
      });

  // Show the ad.
  ad.show(activity);
}

在您准备好展示广告之前,请避免调用 pollAd() 方法。如需在不展示广告的情况下读取广告响应信息,请参阅 读取响应信息

监听广告事件

在展示广告之前,请监听广告事件。以下示例展示了如何为广告事件注册回调:

Kotlin

// Listen for ad events.
val ad = interstitialAd
if (ad == null) {
  Log.e(TAG, "Interstitial ad is not ready yet.")
  return
}

ad.adEventCallback =
  object : InterstitialAdEventCallback {
    override fun onAdShowedFullScreenContent() {
      // Interstitial ad did show.
    }

    override fun onAdDismissedFullScreenContent() {
      // Interstitial ad did dismiss.
      interstitialAd = null
    }

    override fun onAdFailedToShowFullScreenContent(
      fullScreenContentError: FullScreenContentError
    ) {
      // Interstitial ad failed to show.
      Log.e(TAG, "Interstitial ad failed to show: ${fullScreenContentError.message}")
    }

    override fun onAdImpression() {
      // Interstitial ad did record an impression.
    }

    override fun onAdClicked() {
      // Interstitial ad did record a click.
    }
  }

Java

// Listen for ad events.
if (interstitialAd == null) {
  Log.e(TAG, "Interstitial ad is not ready yet.");
  return;
}

interstitialAd.setAdEventCallback(
    new InterstitialAdEventCallback() {
      @Override
      public void onAdShowedFullScreenContent() {
        // Interstitial ad did show.
      }

      @Override
      public void onAdDismissedFullScreenContent() {
        // Interstitial ad did dismiss.
        interstitialAd = null;
      }

      @Override
      public void onAdFailedToShowFullScreenContent(
          @NonNull FullScreenContentError fullScreenContentError) {
        // Interstitial ad failed to show.
        Log.e(TAG, "Interstitial ad failed to show: " + fullScreenContentError.getMessage());
      }

      @Override
      public void onAdImpression() {
        // Interstitial ad did record an impression.
      }

      @Override
      public void onAdClicked() {
        // Interstitial ad did record a click.
      }
    });

可选:监听预加载事件

当您开始预加载广告时,请注册预加载事件,以便在广告预加载成功、预加载失败或广告缓存耗尽时收到通知。

以下示例展示了如何注册预加载广告事件:

Kotlin

val preloadCallback =
  // [Important] Don't call ad preloader start() or pollAd() within the PreloadCallback.
  object : PreloadCallback {
    override fun onAdFailedToPreload(preloadId: String, adError: LoadAdError) {
      Log.d(
        TAG,
        ("Interstitial preload ad $preloadId failed to load with error: ${adError.message}"),
      )
    }

    override fun onAdsExhausted(preloadId: String) {
      Log.i(TAG, "Interstitial preload ad $preloadId is not available")
      // [Important] Don't call ad preloader start() or pollAd() from onAdsExhausted.
    }

    override fun onAdPreloaded(preloadId: String, responseInfo: ResponseInfo) {
      Log.i(TAG, "Interstitial preload ad $preloadId is available")
    }
  }
val adRequest = AdRequest.Builder(adUnitId).build()
val preloadConfig = PreloadConfiguration(adRequest)
InterstitialAdPreloader.start(adUnitId, preloadConfig, preloadCallback)

Java

PreloadCallback preloadCallback =
    // [Important] Don't call ad preloader start() or pollAd() within the PreloadCallback.
    new PreloadCallback() {
      @Override
      public void onAdFailedToPreload(@NonNull String preloadId, @NonNull LoadAdError adError) {
        Log.d(
            TAG,
            String.format(
                "Interstitial preload ad %s failed to load with error: %s",
                preloadId, adError.getMessage()));
        // [Optional] Get the error response info for additional details.
        // ResponseInfo responseInfo = adError.getResponseInfo();
      }

      @Override
      public void onAdsExhausted(@NonNull String preloadId) {
        Log.i(TAG, "Interstitial preload ad " + preloadId + " is not available");
        // [Important] Don't call ad preloader start() or pollAd() from onAdsExhausted.
      }

      @Override
      public void onAdPreloaded(@NonNull String preloadId, @NonNull ResponseInfo responseInfo) {
        Log.i(TAG, "Interstitial preload ad " + preloadId + " is available");
      }
    };

AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest);
InterstitialAdPreloader.start(adUnitId, preloadConfig, preloadCallback);

当广告加载失败时,GMA Next-Gen SDK 会自动预加载广告,并重试 预加载配置的失败请求。

可选:检查广告可用性

如果您需要了解广告是否可用,请检查广告可用性。以下示例展示了如何检查预加载的广告是否可用:

Kotlin

private fun isAdAvailable(adUnitId: String): Boolean {
  return InterstitialAdPreloader.isAdAvailable(adUnitId)
}

Java

private boolean isAdAvailable(String adUnitId) {
  return InterstitialAdPreloader.isAdAvailable(adUnitId);
}

可选:设置缓冲区空间

缓冲区空间用于控制内存中保留的预加载广告数量。默认情况下,Google 会优化缓冲区大小,以平衡内存消耗和广告投放延迟。您可以设置自定义缓冲区空间,以增加内存中保留的广告数量。我们建议缓冲区空间大小不超过 4。

以下示例展示了如何将缓冲区空间大小设置为 4 个预加载的广告:

Kotlin

val adRequest = AdRequest.Builder(adUnitId).build()
// Maintain small or default buffer size unless rapid transitions are expected.
val preloadConfig = PreloadConfiguration(adRequest, bufferSize = 2)
InterstitialAdPreloader.start(adUnitId, preloadConfig)

Java

// Maintain small or default buffer size unless rapid transitions are expected.
AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest, 2);
InterstitialAdPreloader.start(adUnitId, preloadConfig);

预加载缓存限制

GMA Next-Gen SDK 会对所有广告单元和预加载 ID 的预加载广告总数 强制执行应用级限制:

  • 默认限制:Google 会在内存中保留最多 6 个预加载的广告。此限制适用于所有格式和预加载 ID。
  • 我们建议每个预加载 ID 的缓冲区空间为 2 或 3。

可选:停止预加载广告

如果您不需要在会话中再次展示特定预加载 ID 的广告,可以停止预加载广告。如需停止加载特定预加载 ID 的广告,请使用预加载 ID 调用 destroy() 方法。调用 destroy() 方法会从缓存中移除与预加载 ID 关联的所有预加载广告。

以下示例展示了如何停止预加载广告:

Kotlin

private fun stopPreloading(adUnitId: String) {
  // Stops the preloading and destroy preloaded ads.
  InterstitialAdPreloader.destroy(adUnitId)
}

Java

private void stopPreloading(String adUnitId) {
  // Stops the preloading and destroy preloaded ads.
  InterstitialAdPreloader.destroy(adUnitId);
}

可选:读取响应信息

读取下一个预加载的广告的响应信息,而无需从缓存中移除广告。

以下示例展示了如何读取下一个预加载的广告的响应信息:

Kotlin

val responseInfo = InterstitialAdPreloader.peekAdResponseInfo(preloadId)
if (responseInfo == null) {
  Log.e(TAG, "Failed to peek ad response info.")
  return
}

Log.d(TAG, "Peeked ad response ID: ${responseInfo.responseId}")

Java

ResponseInfo responseInfo = InterstitialAdPreloader.peekAdResponseInfo(preloadId);
if (responseInfo == null) {
  Log.e(TAG, "Failed to peek ad response info.");
  return;
}

Log.d(TAG, "Peeked ad response ID: " + responseInfo.getResponseId());