前提条件
完成自定义事件设置。
请求横幅广告
当到达广告瀑布流中介链中的自定义事件订单项时,系统会对您在创建自定义事件时提供的类名称调用 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()
,具体取决于广告是已成功提取还是遇到错误。可通过传入实现 MediationBannerAd
的类的实例来调用 onSuccess()
。
通常,这些方法是在您的适配器所实现的第三方 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 上提供了完整的示例。您可在已获支持的广告联盟上直接使用这些示例代码,也可在修改后用于展示自定义事件横幅广告。