Quảng cáo xen kẽ

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 ở các điểm chuyển tiếp tự nhiên trong quy trình hoạt động 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 độ của trò chơi. Khi một ứng dụng hiển thị quảng cáo xen kẽ, người dùng sẽ có thể chọn nhấn vào quảng cáo và tiếp tục chuyển đến trang đích của quảng cáo đó hoặc đóng quảng cáo và quay lại ứng dụng.

Tài liệu hướng dẫn này giải thích cách tích hợp quảng cáo xen kẽ vào ứng dụng Android.

Điều kiện tiên quyết

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

Khi bạn 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 đang chạy trong 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 đơn giản 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ẽ trên Android:

/21775744923/example/interstitial

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

Để biết thông tin chi tiết về quảng cáo thử nghiệm Google Mobile Ads SDK (Legacy), hãy xem Bật quảng cáo thử nghiệm.

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

Để tải một quảng cáo xen kẽ, hãy gọi phương thức AdManagerInterstitialAd tĩnh load() và truyền AdManagerInterstitialAdLoadCallback để nhận quảng cáo đã tải hoặc bất kỳ lỗi nào có thể xảy ra. Xin lưu ý rằng giống như các lệnh gọi lại tải định dạng khác, AdManagerInterstitialAdLoadCallback tận dụng LoadAdError để cung cấp thông tin chi tiết về lỗi có độ trung thực cao hơn.

Java

AdManagerInterstitialAd.load(
    this,
    AD_UNIT_ID,
    new AdManagerAdRequest.Builder().build(),
    new AdManagerInterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(@NonNull AdManagerInterstitialAd interstitialAd) {
        Log.d(TAG, "Ad was loaded.");
        MyActivity.this.interstitialAd = interstitialAd;
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        Log.d(TAG, loadAdError.getMessage());
        interstitialAd = null;
      }
    });

Kotlin

AdManagerInterstitialAd.load(
  this,
  AD_UNIT_ID,
  AdManagerAdRequest.Builder().build(),
  object : AdManagerInterstitialAdLoadCallback() {
    override fun onAdLoaded(interstitialAd: AdManagerInterstitialAd) {
      Log.d(TAG, "Ad was loaded.")
      this@MyActivity.interstitialAd = interstitialAd
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {
      Log.d(TAG, adError.message)
      interstitialAd = null
    }
  },
)

Đặt lệnh FullScreenContentCallback

FullScreenContentCallback xử lý các sự kiện liên quan đến việc hiển thị InterstitialAd. Trước khi hiển thị InterstitialAd, bạn hãy nhớ đặt lệnh gọi lại:

Java

interstitialAd.setFullScreenContentCallback(
    new FullScreenContentCallback() {
      @Override
      public void onAdDismissedFullScreenContent() {
        // Called when fullscreen content is dismissed.
        Log.d(TAG, "The ad was dismissed.");
        // Make sure to set your reference to null so you don't
        // show it a second time.
        MyActivity.this.interstitialAd = null;
      }

      @Override
      public void onAdFailedToShowFullScreenContent(AdError adError) {
        // Called when fullscreen content failed to show.
        Log.d(TAG, "The ad failed to show.");
        // Make sure to set your reference to null so you don't
        // show it a second time.
        MyActivity.this.interstitialAd = null;
      }

      @Override
      public void onAdShowedFullScreenContent() {
        // Called when fullscreen content is shown.
        Log.d(TAG, "The ad was shown.");
      }

      @Override
      public void onAdImpression() {
        // Called when an impression is recorded for an ad.
        Log.d(TAG, "The ad recorded an impression.");
      }

      @Override
      public void onAdClicked() {
        // Called when ad is clicked.
        Log.d(TAG, "The ad was clicked.");
      }
    });

Kotlin

interstitialAd?.fullScreenContentCallback =
  object : FullScreenContentCallback() {
    override fun onAdDismissedFullScreenContent() {
      // Called when fullscreen content is dismissed.
      Log.d(TAG, "Ad was dismissed.")
      // Don't forget to set the ad reference to null so you
      // don't show the ad a second time.
      interstitialAd = null
    }

    override fun onAdFailedToShowFullScreenContent(adError: AdError) {
      // Called when fullscreen content failed to show.
      Log.d(TAG, "Ad failed to show.")
      // Don't forget to set the ad reference to null so you
      // don't show the ad a second time.
      interstitialAd = null
    }

    override fun onAdShowedFullScreenContent() {
      // Called when fullscreen content is shown.
      Log.d(TAG, "Ad showed fullscreen content.")
    }

    override fun onAdImpression() {
      // Called when an impression is recorded for an ad.
      Log.d(TAG, "Ad recorded an impression.")
    }

    override fun onAdClicked() {
      // Called when ad is clicked.
      Log.d(TAG, "Ad was clicked.")
    }
  }

Hiển thị quảng cáo

Quảng cáo xen kẽ nên xuất hiện tại các điểm tạm dừng tự nhiên trong quy trình hoạt động của ứng dụng. Bạn nên đặt quảng cáo xen kẽ giữa các cấp độ của trò chơi hoặc sau khi người dùng hoàn thành một nhiệm vụ. Để hiển thị quảng cáo xen kẽ, hãy sử dụng phương thức show().

Java

if (interstitialAd != null) {
  interstitialAd.show(this);
} else {
  Log.d(TAG, "The interstitial ad is still loading.");
}

Kotlin

interstitialAd?.show(this)

Ví dụ trên GitHub

  • Ví dụ về quảng cáo xen kẽ: Java | Kotlin

Các bước tiếp theo