必要條件
完成自訂事件設定。
請求橫幅廣告
在瀑布式中介鏈中到達自訂事件明細時,系統會根據您在建立自訂事件時提供的類別名稱,呼叫 loadBannerAd()
方法。在本例中
該方法位於 SampleCustomEvent
中,然後會呼叫 loadBannerAd()
SampleBannerCustomEventLoader
中的方法。
如要要求橫幅廣告,請建立或修改可擴充 Adapter
的類別,以便實作 loadBannerAd()
。此外,請建立新類別來實作 MediationBannerAd
。
在自訂事件範例中,SampleCustomEvent
會擴充 Adapter
類別,然後委派至 SampleBannerCustomEventLoader
。
Java
package com.google.ads.mediation.sample.customevent; import com.google.android.gms.ads.mediation.Adapter; import com.google.android.gms.ads.mediation.MediationAdConfiguration; import com.google.android.gms.ads.mediation.MediationAdLoadCallback; import com.google.android.gms.ads.mediation.MediationBannerAd; import com.google.android.gms.ads.mediation.MediationBannerAdCallback; ... public class SampleCustomEvent extends Adapter { private SampleBannerCustomEventLoader bannerLoader; @Override public void loadBannerAd( @NonNull MediationBannerAdConfiguration adConfiguration, @NonNull MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback> callback) { bannerLoader = new SampleBannerCustomEventLoader(adConfiguration, callback); bannerLoader.loadAd(); } }
SampleBannerCustomEventLoader
負責下列工作:
載入橫幅廣告,並在載入完成後叫用
MediationAdLoadCallback
方法。實作
MediationBannerAd
介面。接收並回報對 Google Mobile Ads SDK 的廣告事件回呼。
UI 中定義的選用參數會納入廣告設定。參數可以透過
adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)
。
這個參數通常是廣告聯播網 SDK 在例項化廣告物件時所需的廣告單元 ID。
Java
package com.google.ads.mediation.sample.customevent; import com.google.android.gms.ads.mediation.Adapter; import com.google.android.gms.ads.mediation.MediationBannerAdConfiguration; import com.google.android.gms.ads.mediation.MediationAdLoadCallback; import com.google.android.gms.ads.mediation.MediationBannerAd; import com.google.android.gms.ads.mediation.MediationBannerAdCallback; ... public class SampleBannerCustomEventLoader extends SampleAdListener implements MediationBannerAd { /** View to contain the sample banner ad. */ private SampleAdView sampleAdView; /** Configuration for requesting the banner ad from the third-party network. */ private final MediationBannerAdConfiguration mediationBannerAdConfiguration; /** Callback that fires on loading success or failure. */ private final MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback> mediationAdLoadCallback; /** Callback for banner ad events. */ private MediationBannerAdCallback bannerAdCallback; /** Constructor. */ public SampleBannerCustomEventLoader( @NonNull MediationBannerAdConfiguration mediationBannerAdConfiguration, @NonNull MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback> mediationAdLoadCallback) { this.mediationBannerAdConfiguration = mediationBannerAdConfiguration; this.mediationAdLoadCallback = mediationAdLoadCallback; } /** Loads a banner ad from the third-party ad network. */ public void loadAd() { // All custom events have a server parameter named "parameter" that returns // back the parameter entered into the UI when defining the custom event. Log.i("BannerCustomEvent", "Begin loading banner ad."); String serverParameter = mediationBannerAdConfiguration.getServerParameters().getString( MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD); Log.d("BannerCustomEvent", "Received server parameter."); Context context = mediationBannerAdConfiguration.getContext(); sampleAdView = new SampleAdView(context); // Assumes that the serverParameter is the ad unit of the Sample Network. sampleAdView.setAdUnit(serverParameter); AdSize size = mediationBannerAdConfiguration.getAdSize(); // Internally, smart banners use constants to represent their ad size, which // means a call to AdSize.getHeight could return a negative value. You can // accommodate this by using AdSize.getHeightInPixels and // AdSize.getWidthInPixels instead, and then adjusting to match the device's // display metrics. int widthInPixels = size.getWidthInPixels(context); int heightInPixels = size.getHeightInPixels(context); DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics(); int widthInDp = Math.round(widthInPixels / displayMetrics.density); int heightInDp = Math.round(heightInPixels / displayMetrics.density); sampleAdView.setSize(new SampleAdSize(widthInDp, heightInDp)); sampleAdView.setAdListener(this); SampleAdRequest request = createSampleRequest(mediationBannerAdConfiguration); Log.i("BannerCustomEvent", "Start fetching banner ad."); sampleAdView.fetchAd(request); } public SampleAdRequest createSampleRequest( MediationAdConfiguration mediationAdConfiguration) { SampleAdRequest request = new SampleAdRequest(); request.setTestMode(mediationAdConfiguration.isTestRequest()); request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet()); return request; } }
視廣告是否成功擷取或發生錯誤而定
會將
onSuccess()
或
onFailure()
。
方法是傳入實作的類別例項,藉此呼叫 onSuccess()
MediationBannerAd
。
通常,這些方法是在來自
您的轉接程式導入的第三方 SDK。在這個範例中
具有包含相關回呼的 SampleAdListener
:
Java
@Override public void onAdFetchSucceeded() { bannerAdCallback = mediationAdLoadCallback.onSuccess(this); } @Override public void onAdFetchFailed(SampleErrorCode errorCode) { mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode)); }
MediationBannerAd
需要實作 View
getter 方法:
Java
@Override @NonNull public View getView() { return sampleAdView; }
將中介服務事件轉送至 Google Mobile Ads SDK
呼叫 onSuccess()
後,傳回的 MediationBannerAdCallback
物件
即可讓轉接器使用
第三方 SDK 整合 Google Mobile Ads SDK。
SampleBannerCustomEventLoader
類別會擴充 SampleAdListener
介面
,將來自範例廣告聯播網的回呼轉送至 Google Mobile Ads SDK。
請務必盡可能轉寄這些回呼,讓應用程式從 Google Mobile Ads SDK 接收這些對等事件。以下是使用回呼的範例:
Java
@Override public void onAdFullScreen() { bannerAdCallback.onAdOpened(); bannerAdCallback.reportAdClicked(); } @Override public void onAdClosed() { bannerAdCallback.onAdClosed(); }
這就完成了橫幅廣告的自訂事件實作。完整範例 會在 GitHub。 您可以將其與已支援的廣告聯播網搭配使用,也可以修改該廣告單元,以便顯示自訂事件橫幅廣告。