插頁式

插頁式廣告會全螢幕顯示,覆蓋整個應用程式的介面。 這類廣告顯示的時間點通常都是在應用程式流程中的自然轉換點, 例如在活動之間或遊戲關卡之間的暫停期間。如果 應用程式顯示插頁式廣告,使用者可選擇輕觸廣告 繼續前往目的地,或關閉應用程式並返回應用程式。

本指南說明如何將插頁式廣告整合至 Flutter 應用程式。

一律使用測試廣告進行測試

建構及測試應用程式時,請務必使用測試廣告,而非 現場及正式環境廣告否則可能導致帳戶遭到停權。

要載入測試廣告,最簡單的方法是使用我們專屬的測試廣告單元編號。 插頁式廣告:

  • /6499/example/interstitial

測試廣告單元會設為針對每個請求傳回測試廣告。 您可以在自己的應用程式中使用這些 API,編寫程式碼、測試及偵錯。 您只要先將這些內容換成自己的廣告單元編號,再發布 應用程式。

載入廣告

以下範例會載入插頁式廣告:

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

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = '/6499/example/interstitial';

  /// Loads an interstitial ad.
  void loadAd() {
    AdManagerInterstitialAd.load(
        adUnitId: adUnitId,
        request: const AdManagerAdRequest(),
        adLoadCallback: AdManagerInterstitialAdLoadCallback(
          // 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('AdManagerInterstitialAd failed to load: $error');
          },
        ));
  }
}

插頁式廣告事件

使用 FullScreenContentCallback 即可監聽生命週期 事件,例如廣告顯示或關閉時間。組合 在顯示廣告前AdManagerInterstitialAd.fullScreenContentCallback 接收這類事件的通知。本範例實作每個方法:

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

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = '/6499/example/interstitial';

  /// Loads an interstitial ad.
  void loadAd() {
    AdManagerInterstitialAd.load(
        adUnitId: adUnitId,
        request: const AdManagerAdRequest(),
        adLoadCallback: AdManagerInterstitialAdLoadCallback(
          // 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('AdManagerInterstitialAd failed to load: $error');
          },
        ));
  }
}

顯示插頁式廣告

AdManagerInterstitialAd 會顯示為 Overlay 放在所有應用程式內容上方並靜態放置;所以無法新增到 Flutter 小工具樹狀結構您可以呼叫 show() 來選擇廣告的放送時機。

_interstitiaAd.show();

呼叫 show() 後,以這種方式顯示的 Ad 便無法關閉 需要使用者輸入內容AdManagerInterstitialAd 一次。後續呼叫也會觸發 onAdFailedToShowFullScreenContent

不再需要放送的廣告時,必須予以處理。最佳做法 何時該呼叫 dispose() 位於 「FullScreenContentCallback.onAdDismissedFullScreenContent」和 FullScreenContentCallback.onAdFailedToShowFullScreenContent 回呼。

大功告成!您的應用程式現在可以顯示插頁式廣告了。

後續步驟