橫幅廣告是會佔用應用程式的部分版面的矩形廣告。他們 會固定在使用者與應用程式互動時,固定在畫面上顯示 使用者捲動畫面時,螢幕頂端或底部,或是內嵌到內容中。橫幅 廣告會在一段時間後自動重新整理查看橫幅廣告總覽 瞭解詳情
本指南說明如何開始使用錨定自動調整橫幅廣告 廣告、 進而盡量提高成效 指定的廣告寬度
錨定自動調整橫幅廣告是固定的顯示比例廣告,而非一般廣告的 固定大小的廣告。長寬比與 320x50 的業界標準相似。一次 請指定可用的完整寬度,系統就會傳回 決定該寬度的高度所有要求,理想的高度不會改變 而且周圍檢視畫面不需要隨著廣告移動 重新整理。
先備知識
- 完成入門指南。
一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非 現場及正式環境廣告否則可能導致帳戶遭到停權。
要載入測試廣告,最簡單的方法是使用我們專屬的測試廣告單元編號。 Android 橫幅:
ca-app-pub-3940256099942544/9214589741
這是經過特別設定,每次請求都會傳回測試廣告。 並在程式設計、測試和偵錯時用於自己的應用程式。只要確定 請先改用您自己的廣告單元 ID,再發布應用程式。
若要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試 Google Ads。
在版面配置中新增 AdView
顯示橫幅的第一步是放置 AdView
在您想要顯示的 Activity
或 Fragment
版面配置中
它:
Java
private AdSize getAdSize() {
// Determine the screen width (less decorations) to use for the ad width.
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float density = outMetrics.density;
float adWidthPixels = adContainerView.getWidth();
// If the ad hasn't been laid out, default to the full screen width.
if (adWidthPixels == 0) {
adWidthPixels = outMetrics.widthPixels;
}
int adWidth = (int) (adWidthPixels / density);
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
private void loadBanner() {
// Create a new ad view.
AdView adView = new AdView(this);
adView.setAdSizes(getAdSize());
adView.setAdUnitId("ca-app-pub-3940256099942544/9214589741");
// Replace ad container with new ad view.
adContainerView.removeAllViews();
adContainerView.addView(adView);
// Start loading the ad in the background.
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
Kotlin
// Determine the screen width (less decorations) to use for the ad width.
// If the ad hasn't been laid out, default to the full screen width.
private val adSize: AdSize
get() {
val display = windowManager.defaultDisplay
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val density = outMetrics.density
var adWidthPixels = binding.adViewContainer.width.toFloat()
if (adWidthPixels == 0f) {
adWidthPixels = outMetrics.widthPixels.toFloat()
}
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
private fun loadBanner() {
// Create a new ad view.
val adView = AdView(this)
adView.adSizes = adSize
adView.adUnitId = "ca-app-pub-3940256099942544/9214589741"
// Create an ad request.
val adRequest = AdRequest.Builder().build()
// Start loading the ad in the background.
adView.loadAd(adRequest)
}
載入廣告
AdView 設定完成後
並載入廣告這就是loadAd()
AdView
類別中的方法。這需要 AdRequest
參數,用來儲存
單一廣告請求
以下範例說明如何在onCreate()
Activity
:
Java
private void loadBanner() {
// Create a new ad view.
adView = new AdView(this);
adView.setAdUnitId(AD_UNIT);
adView.setAdSize(getAdSize());
// Replace ad container with new ad view.
adContainerView.removeAllViews();
adContainerView.addView(adView);
// Start loading the ad in the background.
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
Kotlin
private fun loadBanner() {
// This is an ad unit ID for a test ad. Replace with your own banner ad unit ID.
adView.adUnitId = "/6499/example/banner"
adView.setAdSize(adSize)
// Create an ad request.
val adRequest = AdRequest.Builder().build()
// Start loading the ad in the background.
adView.loadAd(adRequest)
}
如果廣告無法載入,您不需要明確請求其他廣告 前提是廣告單元已經設定重新整理Google Mobile Ads SDK 採用您在 AdMob 中指定的任何重新整理頻率 網頁介面上。如果尚未啟用重新整理功能,就必須發布新的 請求。
大功告成!您的應用程式現在可以顯示橫幅廣告了。
廣告事件
您可以監聽廣告生命週期中的多個事件,包括載入、 廣告曝光和點擊事件,以及廣告開啟和關閉事件。建議 來設定回呼,再載入橫幅廣告。Java
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(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
類別。
啟用硬體加速
如果開啟硬體加速功能時,應用程式無法正常運作
也可以控制個別活動的位置。如要啟用或
停用硬體加速時,您可以使用 android:hardwareAccelerated
屬性的
<application>
和
<activity>
AndroidManifest.xml
中的元素。以下範例會啟用硬體
但會針對單一活動停用此功能:
<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>
請參閱硬體加速 指南 控制硬體加速的選項。請注意, 如果活動發生了硬體加速功能,您就無法啟用個別廣告觀看次數 因此已停用,因此活動本身必須啟用硬體加速功能。
其他資源
GitHub 上的範例
後續步驟
可收合橫幅廣告
可收合橫幅廣告是一種橫幅廣告,一開始會以較大尺寸呈現 重疊廣告,還有可將廣告收合成較小尺寸的按鈕。建議採用 進一步提升成效詳情請參閱「可收合橫幅廣告」一文。
內嵌自動調整橫幅廣告
與錨定自動調整橫幅廣告相比,內嵌自動調整橫幅廣告較大、尺寸更加大 橫幅廣告它們的高度不一,而且可以和裝置螢幕一樣高。 建議內嵌自動調整橫幅廣告,而非錨定式自動調整橫幅廣告。 應用程式會在可捲動內容中顯示橫幅廣告。請參閱內嵌自動調整資源配置 橫幅廣告 詳細資料。