前提条件
完成自定义事件设置。
请求横幅广告
在达到广告瀑布流中介链内的自定义事件订单项时,系统会对您在创建自定义事件时提供的类名称调用 loadBannerAd()
方法。在这种情况下,该方法位于 SampleCustomEvent
中,后者随后会调用 SampleBannerCustomEventLoader
中的 loadBannerAd()
方法。
要请求横幅广告,请创建或修改一个将 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 移动广告 SDK 报告。
界面中定义的可选参数会包含在广告配置中。此参数可通过 adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)
进行访问。此参数通常是广告联盟 SDK
所需的资源。
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 的回调中实现的。在本示例中,示例 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 移动广告 SDK
调用 onSuccess()
后,适配器便可以使用返回的 MediationBannerAdCallback
对象,将展示事件从第三方 SDK 转发至 Google 移动广告 SDK。通过
SampleBannerCustomEventLoader
类扩展了 SampleAdListener
接口
将示例广告联盟中的回调转发给 Google 移动广告 SDK。
请务必确保您的自定义事件转发的回调数量与 这样您的应用才能从 Google 移动广告 SDK。以下示例展示了如何使用回调:
Java
@Override public void onAdFullScreen() { bannerAdCallback.onAdOpened(); bannerAdCallback.reportAdClicked(); } @Override public void onAdClosed() { bannerAdCallback.onAdClosed(); }
到这里,我们已经实现针对横幅广告的自定义事件。完整示例 可通过以下平台观看: GitHub。 您可以将其用于已获支持的广告网络,也可以将其修改为 展示自定义事件横幅广告。