插页式广告

插页式广告是全屏广告,会覆盖宿主应用的整个界面。它们通常在应用流程的自然过渡点展示,例如,活动之间的切换处或游戏关卡之间的暂停时段。当应用展示插页式广告时,用户可以选择点按广告,访问其目标网址,也可以将其关闭,返回应用。

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

始终使用测试广告进行测试

在构建和测试应用时,请确保使用测试广告,而不是实际投放的广告。否则,可能会导致您的帐号被暂停。

对于插页式广告,加载测试广告最简便的方法就是使用下面的测试专用广告单元 ID:

Android

ca-app-pub-3940256099942544/1033173712

iOS

ca-app-pub-3940256099942544/4411468910

测试广告单元配置为为每个请求返回测试广告,您可以在自己应用的编码、测试和调试过程中随意使用这些测试广告单元。只需确保在发布应用前用您自己的广告单元 ID 替换这些测试广告单元 ID 即可。

加载广告

以下示例加载了一个插页式广告:

class InterstitialExampleState extends State<InterstitialExample> {
  InterstitialAd? _interstitialAd;

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = Platform.isAndroid
    ? 'ca-app-pub-3940256099942544/1033173712'
    : 'ca-app-pub-3940256099942544/4411468910';

  /// Loads an interstitial ad.
  void loadAd() {
    InterstitialAd.load(
        adUnitId: adUnitId,
        request: const AdRequest(),
        adLoadCallback: InterstitialAdLoadCallback(
          // Called when an ad is successfully received.
          onAdLoaded: (ad) {
            debugPrint('$ad loaded.');
            // Keep a reference to the ad so you can show it later.
            _interstitialAd = ad;
          },
          // Called when an ad request failed.
          onAdFailedToLoad: (LoadAdError error) {
            debugPrint('InterstitialAd failed to load: $error');
          },
        ));
  }
}

插页式广告事件

通过使用 FullScreenContentCallback,您可以监听各种广告生命周期事件,例如广告何时展示或何时关闭。在展示广告之前设置 InterstitialAd.fullScreenContentCallback,以接收这些事件的通知。以下示例实现了每个方法:

class InterstitialExampleState extends State<InterstitialExample> {
  InterstitialAd? _interstitialAd;

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = Platform.isAndroid
    ? 'ca-app-pub-3940256099942544/1033173712'
    : 'ca-app-pub-3940256099942544/4411468910';

  /// Loads an interstitial ad.
  void loadAd() {
    InterstitialAd.load(
        adUnitId: adUnitId,
        request: const AdRequest(),
        adLoadCallback: InterstitialAdLoadCallback(
          // Called when an ad is successfully received.
          onAdLoaded: (ad) {
             ad.fullScreenContentCallback = FullScreenContentCallback(
                // Called when the ad showed the full screen content.
                onAdShowedFullScreenContent: (ad) {},
                // Called when an impression occurs on the ad.
                onAdImpression: (ad) {},
                // Called when the ad failed to show full screen content.
                onAdFailedToShowFullScreenContent: (ad, err) {
                  // Dispose the ad here to free resources.
                  ad.dispose();
                },
                // Called when the ad dismissed full screen content.
                onAdDismissedFullScreenContent: (ad) {
                  // Dispose the ad here to free resources.
                  ad.dispose();
                },
                // Called when a click is recorded for an ad.
                onAdClicked: (ad) {});

            debugPrint('$ad loaded.');
            // Keep a reference to the ad so you can show it later.
            _interstitialAd = ad;
          },
          // Called when an ad request failed.
          onAdFailedToLoad: (LoadAdError error) {
            debugPrint('InterstitialAd failed to load: $error');
          },
        ));
  }
}

展示插页式广告

InterstitialAd 会在所有应用内容之上显示为 Overlay,并以静态方式放置;因此,它无法添加到 Flutter widget 树。您可以通过调用 show() 来选择展示广告的时间。

_interstitiaAd.show();

调用 show() 后,以这种方式展示的 Ad 无法以编程方式关闭,并且需要用户输入。InterstitialAd 只能显示一次。后续显示的调用将触发 onAdFailedToShowFullScreenContent

如果不再需要访问某个广告,则必须予以处置。调用 dispose() 的最佳实践是在 FullScreenContentCallback.onAdDismissedFullScreenContentFullScreenContentCallback.onAdFailedToShowFullScreenContent 回调中进行。

大功告成!现在,您的应用就可以展示插页式广告了。

后续步骤

GitHub 上的完整示例

插页式广告