Trung gian

Quảng cáo xen kẽ là quảng cáo toàn màn hình, che phủ giao diện của ứng dụng lưu trữ. Quảng cáo này thường xuất hiện tại các điểm chuyển tiếp tự nhiên trong luồng thực thi của ứng dụng, chẳng hạn như giữa các hoạt động hoặc trong thời gian tạm dừng giữa các cấp độ trong trò chơi. Khi một ứng dụng hiển thị quảng cáo xen kẽ, người dùng có thể chọn nhấn vào quảng cáo và tiếp tục tới đích đến của ứng dụng hoặc đóng ứng dụng và quay lại ứng dụng.

Hướng dẫn này giải thích cách tích hợp quảng cáo xen kẽ vào một ứng dụng Flutter.

Luôn thử nghiệm bằng quảng cáo thử nghiệm

Khi tạo và thử nghiệm ứng dụng, hãy nhớ sử dụng quảng cáo thử nghiệm thay vì quảng cáo thực tế. Chúng tôi có thể tạm ngưng tài khoản của bạn nếu bạn không làm như vậy.

Cách dễ nhất để tải quảng cáo thử nghiệm là sử dụng mã đơn vị quảng cáo thử nghiệm dành riêng cho quảng cáo xen kẽ:

  • /6499/example/interstitial

Các đơn vị quảng cáo thử nghiệm được định cấu hình để trả về quảng cáo thử nghiệm cho mọi yêu cầu, và bạn có thể sử dụng chúng trong ứng dụng của riêng mình khi lập trình, thử nghiệm và gỡ lỗi. Bạn chỉ cần nhớ thay thế các mã này bằng mã đơn vị quảng cáo của riêng mình trước khi xuất bản .

Tải một quảng cáo

Sau đây là ví dụ về đoạn mã tải một quảng cáo xen kẽ:

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');
          },
        ));
  }
}

Sự kiện của quảng cáo xen kẽ

Khi sử dụng FullScreenContentCallback, bạn có thể theo dõi vòng đời các sự kiện, chẳng hạn như khi quảng cáo hiển thị hoặc bị đóng. Đặt AdManagerInterstitialAd.fullScreenContentCallback trước khi hiển thị quảng cáo để nhận cho các sự kiện này. Ví dụ sau triển khai từng phương thức:

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');
          },
        ));
  }
}

Hiển thị quảng cáo xen kẽ

AdManagerInterstitialAd hiển thị dưới dạng Overlay ở đầu tất cả nội dung ứng dụng và được đặt cố định; do đó, bạn không thể thêm thông tin này vào cây tiện ích Flutter. Bạn có thể chọn thời điểm hiển thị quảng cáo bằng cách gọi show().

_interstitiaAd.show();

Sau khi gọi show(), bạn sẽ không thể đóng Ad hiển thị theo cách này theo phương thức lập trình và yêu cầu hoạt động đầu vào của người dùng. Chỉ có thể hiển thị AdManagerInterstitialAd một lần. Các lệnh gọi tiếp theo để hiển thị sẽ kích hoạt onAdFailedToShowFullScreenContent.

Bạn phải loại bỏ quảng cáo khi không cần truy cập vào quảng cáo đó nữa. Phương pháp hay nhất để biết thời điểm gọi dispose() là trong FullScreenContentCallback.onAdDismissedFullScreenContent và Các lệnh gọi lại FullScreenContentCallback.onAdFailedToShowFullScreenContent.

Vậy là xong! Bây giờ, ứng dụng của bạn đã sẵn sàng để hiển thị quảng cáo xen kẽ.

Các bước tiếp theo