廣告預先載入是由 SDK 管理的載入和快取程序,可省去手動載入和快取廣告的麻煩。這個程序可讓您在需要時顯示廣告,不必手動載入廣告或處理廣告載入回呼。
本指南說明如何設定預先載入廣告、檢查預先載入廣告是否可用,以及如何取得並顯示預先載入廣告。
必要條件
繼續進行本教學課程前,請先完成下列項目:
- 安裝 Google Mobile Ads SDK (Beta 版) 0.14.0-alpha01 以上版本。
- 完成入門指南的步驟。
- 選用:下載並執行範例應用程式。
開始預先載入廣告
如要開始預先載入,請呼叫 startPreload()。Google Mobile Ads SDK 會自動重試預先載入設定的失敗廣告請求。
以下範例會開始預先載入廣告:
Kotlin
private fun startPreloading(adUnitID: String) {
val adRequest: AdRequest = AdRequest.Builder(adUnitID).build()
val preloadConfig = PreloadConfiguration(adRequest)
InterstitialAdPreloader.start(adUnitID, preloadConfig)
}
Java
private void startPreloading(String adUnitId) {
AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest);
InterstitialAdPreloader.start(adUnitId, preloadConfig);
}
選用:修改預先載入廣告
如要變更預先載入的廣告,請呼叫 destroy(),然後使用修改後的設定再次呼叫 startPreload()。
選用:停止預先載入廣告
如要停止預先載入廣告,請使用預先載入 ID 呼叫 destroy()。
選用:設定緩衝區大小
緩衝區大小會控管記憶體中預先載入的廣告數量。如果應用程式顯示廣告的速度比載入新廣告快,請將緩衝區大小設為較大的值。如果擔心記憶體用量,請將緩衝區大小設為較小的值,例如 2。
Kotlin
private fun setBufferSize(adUnitID: String) {
val adRequest: AdRequest = AdRequest.Builder(adUnitID).build()
val preloadConfig = PreloadConfiguration(adRequest, bufferSize = 3)
InterstitialAdPreloader.start(adUnitID, preloadConfig)
}
Java
private void setBufferSize(String adUnitId) {
AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest, 3);
InterstitialAdPreloader.start(adUnitId, preloadConfig);
}
選用:接收預先載入通知
實作 PreloadCallback,接收廣告預先載入事件的通知。如要偵錯和記錄,這些事件會提供回應資訊物件。
Kotlin
private fun startPreloadingWithCallback(adUnitID: String) {
val preloadCallback =
// [Important] Don't call ad preloader start() or pollAd() within the PreloadCallback.
object : PreloadCallback {
override fun onAdFailedToPreload(preloadId: String, adError: LoadAdError) {
Log.i(
TAG,
("Interstitial preload ad $preloadId failed to load with error: ${adError.message}"),
)
// [Optional] Get the error response info for additional details.
// val responseInfo = adError.responseInfo
}
override fun onAdsExhausted(preloadId: String) {
Log.i(TAG, "Interstitial preload ad $preloadId is not available")
}
override fun onAdPreloaded(preloadId: String, responseInfo: ResponseInfo) {
Log.i(TAG, "Interstitial preload ad $preloadId is available")
}
}
val adRequest: AdRequest = AdRequest.Builder(adUnitID).build()
val preloadConfig = PreloadConfiguration(adRequest)
InterstitialAdPreloader.start(adUnitID, preloadConfig, preloadCallback)
}
Java
private void startPreloadingWithCallback(String adUnitId) {
PreloadCallback preloadCallback =
// [Important] Don't call ad preloader start() or pollAd() within the PreloadCallback.
new PreloadCallback() {
@Override
public void onAdFailedToPreload(String preloadId, LoadAdError adError) {
Log.e(
TAG,
String.format(
"Interstitial preload ad %s failed to load with error: %s",
preloadId, adError.getMessage()));
// [Optional] Get the error response info for additional details.
// ResponseInfo responseInfo = adError.getResponseInfo();
}
@Override
public void onAdsExhausted(String preloadId) {
Log.i(TAG, "Interstitial preload ad " + preloadId + " is not available");
}
@Override
public void onAdPreloaded(String preloadId, ResponseInfo responseInfo) {
Log.i(TAG, "Interstitial preload ad " + preloadId + " is available");
}
};
AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest);
InterstitialAdPreloader.start(adUnitId, preloadConfig, preloadCallback);
}
取得並顯示預先載入廣告
呼叫 onAdsAvailable() 後,即可顯示廣告。廣告放送完畢後,系統會持續提供廣告。取得廣告後,請監聽廣告生命週期事件並顯示廣告。
以下範例顯示預先載入的廣告:
Kotlin
private fun pollAndShowAd(activity: Activity, adUnitID: String) {
// Polling returns the next available ad and loads another ad in the background.
val ad = InterstitialAdPreloader.pollAd(adUnitID)
// Interact with the ad object as needed.
ad?.apply {
Log.d(TAG, "Interstitial ad response info: ${this.getResponseInfo()}")
this.adEventCallback =
object : InterstitialAdEventCallback {
override fun onAdImpression() {
Log.d(TAG, "Interstitial ad recorded an impression.")
}
override fun onAdPaid(value: AdValue) {
Log.d(TAG, "Interstitial ad onPaidEvent: ${value.valueMicros} ${value.currencyCode}")
}
}
// Show the ad.
ad.show(activity)
}
}
Java
private void pollAndShowAd(Activity activity, String adUnitId) {
// Polling returns the next available ad and loads another ad in the background.
final InterstitialAd ad = InterstitialAdPreloader.pollAd(adUnitId);
// Interact with the ad object as needed.
if (ad != null) {
Log.d(TAG, "Interstitial ad response info: " + ad.getResponseInfo());
ad.setAdEventCallback(
new InterstitialAdEventCallback() {
@Override
public void onAdImpression() {
Log.d(TAG, "Interstitial ad recorded an impression.");
}
@Override
public void onAdPaid(AdValue value) {
Log.d(
TAG,
"Interstitial ad onPaidEvent: "
+ value.getValueMicros()
+ " "
+ value.getCurrencyCode());
}
});
// Show the ad.
ad.show(activity);
}
}
選用:檢查預先載入廣告是否可用
如要瞭解廣告是否可用,請撥打 isAdAvailable:
Kotlin
private fun isAdAvailable(adUnitID: String): Boolean {
return InterstitialAdPreloader.isAdAvailable(adUnitID)
}
Java
private boolean isAdAvailable(String adUnitId) {
return InterstitialAdPreloader.isAdAvailable(adUnitId);
}