橫幅廣告

選取平台: Android iOS Unity Flutter

橫幅廣告會顯示在應用程式版面的一部分矩形區塊。錨定式自動調整橫幅廣告的長寬比固定,使用者與應用程式互動時,這類廣告會持續顯示於畫面頂端或底部。

本指南說明如何在 Android 應用程式中載入錨定式自動調整橫幅廣告。

先決條件

  • 完成入門指南的步驟。
  • (選用) 如要查看橫幅廣告導入範例,請選取下列任一範例應用程式:

請務必使用測試廣告進行測試

建構及測試應用程式時,請務必使用測試廣告,而非實際運作中的廣告,否則可能導致帳戶遭停權。

如要載入測試廣告,最簡單的方法是使用 Android 橫幅專用的測試廣告單元 ID:

ca-app-pub-3940256099942544/9214589741

測試 ID 經特別設定,可針對每個請求傳回測試廣告,且您可在編寫程式碼、測試及偵錯時,在自家應用程式中使用這些 ID。發布應用程式前,請務必將測試用 ID 替換為自己的廣告單元 ID。

請參閱「啟用測試廣告」,進一步瞭解 Google Mobile Ads SDK 的測試廣告運作方式。

定義廣告檢視區塊

XML 版面配置

在版面配置 XML 檔案中新增檢視區塊,做為錨定式自動調整橫幅廣告的容器:

<!-- Ad view container that fills the width of the screen and adjusts its
    height to the content of the ad. -->
<FrameLayout
        android:id="@+id/ad_view_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true" />

Jetpack Compose

  1. 加入 JetpackComposeDemo/compose-util 模組。這個模組包含用於編寫 AdView 物件和素材資源的輔助程式。

  2. 使用 compose-util 模組編寫 BannerAd 類別:


// Place the ad view at the bottom of the screen.
Column(modifier = modifier.fillMaxSize(), verticalArrangement = Arrangement.Bottom) {
  Box(modifier = modifier.fillMaxWidth()) { BannerAd(adView, modifier) }
}

設定廣告大小

AdSize 設為錨定式自動調整橫幅廣告,並指定寬度:

Java

// Request an anchored adaptive banner with a width of 360.
adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360));

Kotlin

// Request an anchored adaptive banner with a width of 360.
adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360))

Jetpack Compose


// Set the adaptive banner ad size with a given width.
val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(LocalContext.current, 360)
adView.setAdSize(adSize)

AdView 新增至版面配置

根據要加到應用程式版面配置的廣告大小,建立 AdView

Java


// Create a new ad view.
adView = new AdView(this);
adView.setAdUnitId(AD_UNIT_ID);
// Request an anchored adaptive banner with a width of 360.
adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360));

// Replace ad container with new ad view.
adContainerView.removeAllViews();
adContainerView.addView(adView);

Kotlin


// Create a new ad view.
val adView = AdView(this)
adView.adUnitId = AD_UNIT_ID
// Request an anchored adaptive banner with a width of 360.
adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360))
this.adView = adView

// Replace ad container with new ad view.
binding.adViewContainer.removeAllViews()
binding.adViewContainer.addView(adView)

Jetpack Compose


val adView = remember { AdView(context) }

// Setup and load the adview.
// Set the unique ID for this specific ad unit.
adView.adUnitId = BANNER_AD_UNIT_ID

// Set the adaptive banner ad size with a given width.
val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(LocalContext.current, 360)
adView.setAdSize(adSize)

// Place the ad view at the bottom of the screen.
Column(modifier = modifier.fillMaxSize(), verticalArrangement = Arrangement.Bottom) {
  Box(modifier = modifier.fillMaxWidth()) { BannerAd(adView, modifier) }
}

載入廣告

AdView 建立完成後,接著就要載入廣告,做法是使用 AdView 類別的 loadAd() 方法。這個方法需要 AdRequest 參數,用來存放有關單一廣告請求的執行階段資訊,例如指定目標資訊。

以下是載入廣告的範例:

Java

AdManagerAdRequest adRequest = new AdManagerAdRequest.Builder().build();
adView.loadAd(adRequest);

Kotlin

val adRequest = AdRequest.Builder().build()
adView.loadAd(adRequest)

如果順利載入,應用程式就能顯示橫幅廣告。

重新整理廣告

如已設定廣告單元重新整理功能,廣告載入失敗時,就不需要請求其他廣告。Google Mobile Ads SDK 會套用您在 AdMob UI 指定的重新整理頻率。如果您未啟用重新整理功能,請發出新請求。請參閱「為橫幅廣告使用自動重新整理功能」一文,進一步瞭解廣告單元重新整理功能 (例如設定重新整理頻率)。

釋出廣告資源

放送完橫幅廣告後,您可以釋出橫幅廣告的資源。

如要釋出廣告資源,請從檢視區塊階層移除廣告,並捨棄所有參照項目:

Java

public void destroyBanner() {
  // Remove banner from view hierarchy.
  if (adView != null) {
    View parentView = (View) adView.getParent();
    if (parentView instanceof ViewGroup) {
      ((ViewGroup) parentView).removeView(adView);
    }

    // Destroy the banner ad resources.
    adView.destroy();
  }

  // Drop reference to the banner ad.
  adView = null;
}

Kotlin

fun destroyBanner() {
  // Remove banner from view hierarchy.
  val parentView = adView?.parent
  if (parentView is ViewGroup) {
    parentView.removeView(adView)
  }

  // Destroy the banner ad resources.
  adView?.destroy()

  // Drop reference to the banner ad.
  adView = null
}

廣告事件

您可以監聽廣告生命週期中的多種事件,包括載入、廣告曝光和點擊,以及廣告開啟和關閉事件。建議您先設定回呼,再載入橫幅廣告。

Java

if (adView != null) {
  adView.setAdListener(
      new AdListener() {
        @Override
        public void onAdClicked() {
          // Code to be executed when the user clicks on an ad.
        }

        @Override
        public void onAdClosed() {
          // Code to be executed when the user is about to return
          // to the app after tapping on an ad.
        }

        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError adError) {
          // Code to be executed when an ad request fails.
        }

        @Override
        public void onAdImpression() {
          // Code to be executed when an impression is recorded
          // for an ad.
        }

        @Override
        public void onAdLoaded() {
          // Code to be executed when an ad finishes loading.
        }

        @Override
        public void onAdOpened() {
          // Code to be executed when an ad opens an overlay that
          // covers the screen.
        }
      });
}

Kotlin

adView?.adListener =
  object : AdListener() {
    override fun onAdClicked() {
      // Code to be executed when the user clicks on an ad.
    }

    override fun onAdClosed() {
      // Code to be executed when the user is about to return
      // to the app after tapping on an ad.
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {
      // Code to be executed when an ad request fails.
    }

    override fun onAdImpression() {
      // Code to be executed when an impression is recorded
      // for an ad.
    }

    override fun onAdLoaded() {
      // Code to be executed when an ad finishes loading.
    }

    override fun onAdOpened() {
      // Code to be executed when an ad opens an overlay that
      // covers the screen.
    }
  }

AdListener 中每個可覆寫的方法,都會對應至廣告生命週期中的一個事件。

可覆寫的方法
onAdClicked() 記錄到廣告點擊時,系統會叫用 onAdClicked() 方法。
onAdClosed() 當使用者查看了廣告的到達網頁網址,再返回應用程式時,系統會叫用 onAdClosed() 方法。應用程式可使用此方法,重啟已停權的活動或執行其他必要工作,做好互動準備。請參閱 AdMob AdListener 範例,瞭解如何在 Android API 試用版應用程式中導入廣告事件監聽器方法。
onAdFailedToLoad() onAdFailedToLoad() 是唯一包含參數的方法。LoadAdError 類型的錯誤參數會說明發生哪些錯誤。詳情請參閱偵錯廣告載入錯誤說明文件
onAdImpression() 記錄到廣告曝光時,系統會叫用 onAdImpression() 方法。
onAdLoaded() onAdLoaded() 方法會在廣告載入完成後執行。如果您希望在廣告確定載入後,才將 AdView 加到活動或片段中,可以在此階段執行這項操作。
onAdOpened() 廣告開啟占用畫面的疊加層時,系統會叫用 onAdOpened() 方法。

影片廣告的硬體加速功能

為確保橫幅廣告檢視區塊的影片廣告順利顯示,請務必啟用硬體加速

硬體加速功能預設為啟用,但部分應用程式可能會選擇停用。如果您的應用程式停用硬體加速,對於會使用廣告的 Activity 類別,建議您啟用這項功能。

啟用硬體加速功能

如果啟用全域硬體加速,會使應用程式無法正常運作,可針對個別活動啟用這項功能。如要啟用或停用硬體加速,請在 AndroidManifest.xml 中,針對 <application><activity> 元素使用 android:hardwareAccelerated 屬性。請參考以下範例,為整個應用程式啟用硬體加速,但對特定活動停用該功能:

<application android:hardwareAccelerated="true">
    <!-- For activities that use ads, hardwareAcceleration should be true. -->
    <activity android:hardwareAccelerated="true" />
    <!-- For activities that don't use ads, hardwareAcceleration can be false. -->
    <activity android:hardwareAccelerated="false" />
</application>

請參閱硬體加速指南,進一步瞭解硬體加速的控管選項。請注意,如果停用 Activity 類別,就無法啟用個別廣告檢視區塊的硬體加速功能,因此須針對 Activity 啟用硬體加速。

後續步驟

可收合橫幅廣告

「可收合橫幅廣告」一開始顯示時,會以較大尺寸重疊在畫面上,當使用者點按收合按鈕,廣告就會恢復為較小尺寸。建議使用這項功能,進一步提升成效。詳情請參閱「可收合橫幅廣告」一文。

內嵌自動調整橫幅廣告

與錨定式自動調整橫幅廣告相比,內嵌自動調整橫幅廣告的版面更大、更高,且高度可變,甚至可與裝置螢幕同高。如果應用程式會在可捲動內容中放送橫幅廣告,內嵌自動調整橫幅廣告會是比錨定式更好的選擇。詳情請參閱「內嵌自動調整橫幅廣告」一文。

探索其他主題