插页式广告自定义事件
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
前提条件
完成自定义事件设置。
请求插页式广告
在达到广告瀑布流中介链内的自定义事件订单项时,系统会对您在创建自定义事件时提供的类名称调用 loadInterstitialAd()
方法。在这种情况下,该方法位于 SampleCustomEvent
中,后者随后会调用 SampleInterstitialCustomEventLoader
中的 loadInterstitialAd()
方法。
若要请求插页式广告,请创建或修改可扩展 Adapter
的类,以实现 loadInterstitialAd()
。此外,请创建一个新类来实现 MediationInterstitialAd
。
在我们的自定义事件示例中,SampleCustomEvent
会扩展 Adapter
类,然后委托给 SampleInterstitialCustomEventLoader
。
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.MediationInterstitialAd;
import com.google.android.gms.ads.mediation.MediationInterstitialAdCallback;
...
public class SampleCustomEvent extends Adapter {
private SampleInterstitialCustomEventLoader interstitialLoader;
@Override
public void loadInterstitialAd(
@NonNull MediationInterstitialAdConfiguration adConfiguration,
@NonNull
MediationAdLoadCallback<MediationInterstitialAd, MediationInterstitialAdCallback>
callback) {
interstitialLoader = new SampleInterstitialCustomEventLoader(adConfiguration, callback);
interstitialLoader.loadAd();
}
}
SampleInterstitialCustomEventLoader
负责执行以下任务:
Ad Manager 界面中定义的可选参数包含在广告配置中。此参数可通过 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.MediationInterstitialAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationInterstitialAd;
import com.google.android.gms.ads.mediation.MediationInterstitialAdCallback;
...
public class SampleInterstitialCustomEventLoader extends SampleAdListener
implements MediationInterstitialAd {
/** A sample third-party SDK interstitial ad. */
private SampleInterstitial sampleInterstitialAd;
/** Configuration for requesting the interstitial ad from the third-party network. */
private final MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration;
/** Callback for interstitial ad events. */
private MediationInterstitialAdCallback interstitialAdCallback;
/** Callback that fires on loading success or failure. */
private final MediationAdLoadCallback<MediationInterstitialAd, MediationInterstitialAdCallback>
mediationAdLoadCallback;
/** Constructor. */
public SampleInterstitialCustomEventLoader(
@NonNull MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration,
@NonNull MediationAdLoadCallback<MediationInterstitialAd, MediationInterstitialAdCallback>
mediationAdLoadCallback) {
this.mediationInterstitialAdConfiguration = mediationInterstitialAdConfiguration;
this.mediationAdLoadCallback = mediationAdLoadCallback;
}
/** Loads the interstitial 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("InterstitialCustomEvent", "Begin loading interstitial ad.");
String serverParameter = mediationInterstitialAdConfiguration.getServerParameters().getString(
MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD);
Log.d("InterstitialCustomEvent", "Received server parameter.");
sampleInterstitialAd =
new SampleInterstitial(mediationInterstitialAdConfiguration.getContext());
sampleInterstitialAd.setAdUnit(serverParameter);
// Implement a SampleAdListener and forward callbacks to mediation.
sampleInterstitialAd.setAdListener(this);
// Make an ad request.
Log.i("InterstitialCustomEvent", "start fetching interstitial ad.");
sampleInterstitialAd.fetchAd(
SampleCustomEvent.createSampleRequest(mediationInterstitialAdConfiguration));
}
public SampleAdRequest createSampleRequest(
MediationAdConfiguration mediationAdConfiguration) {
SampleAdRequest request = new SampleAdRequest();
request.setTestMode(mediationAdConfiguration.isTestRequest());
request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet());
return request;
}
}
无论广告是已成功提取还是遇到错误,您都需要调用 onSuccess()
或 onFailure()
。通过传入实现 MediationInterstitialAd
的类的实例来调用 onSuccess()
。
通常,这些方法是在您的适配器所实现的第三方 SDK 的回调中实现的。在此示例中,示例 SDK 具有一个 SampleAdListener
及相关回调:
Java
@Override
public void onAdFetchSucceeded() {
interstitialAdCallback = mediationAdLoadCallback.onSuccess(this);
}
@Override
public void onAdFetchFailed(SampleErrorCode errorCode) {
mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));
}
MediationInterstitialAd
需要实现 showAd()
方法来展示广告:
Java
@Override
public void showAd(@NonNull Context context) {
sampleInterstitialAd.show();
}
调用 onSuccess()
后,适配器便可以使用返回的 MediationInterstitialAdCallback
对象,将展示事件从第三方 SDK 转发至 Google 移动广告 SDK。SampleInterstitialCustomEventLoader
类扩展了 SampleAdListener
接口,可将回调从示例广告联盟转发到 Google 移动广告 SDK。
您的自定义事件必须尽可能多地转发这些回调,以便您的应用从 Google 移动广告 SDK 接收这些等效事件。以下示例展示了如何使用回调:
Java
@Override
public void onAdFullScreen() {
interstitialAdCallback.reportAdImpression();
interstitialAdCallback.onAdOpened();
}
@Override
public void onAdClosed() {
interstitialAdCallback.onAdClosed();
}
到这里,我们已经实现针对插页式广告的自定义事件。GitHub提供了完整的示例。您可在已获支持的广告联盟上直接使用这些示例代码,也可在修改后用于展示自定义事件插页式广告。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eTo request an interstitial ad via a custom event, extend the \u003ccode\u003eAdapter\u003c/code\u003e class and implement the \u003ccode\u003eloadInterstitialAd()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eCreate a separate class that implements \u003ccode\u003eMediationInterstitialAd\u003c/code\u003e to handle ad loading, display, and event reporting.\u003c/p\u003e\n"],["\u003cp\u003eForward ad events like impressions, clicks, and closes from your custom event to the Google Mobile Ads SDK using the \u003ccode\u003eMediationInterstitialAdCallback\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eLeverage the server parameter defined in the Ad Manager UI, often an ad unit ID, to configure your ad requests within the custom event.\u003c/p\u003e\n"],["\u003cp\u003eThe provided sample code demonstrates a complete custom event implementation for interstitial ads, which can be adapted for your specific ad network.\u003c/p\u003e\n"]]],["To request an interstitial ad, create a class extending `Adapter` and implement `loadInterstitialAd()`, and another implementing `MediationInterstitialAd`. In `loadInterstitialAd()`, instantiate a loader class (e.g., `SampleInterstitialCustomEventLoader`) to load the ad and handle callbacks. This loader uses `MediationAdLoadCallback` to report success or failure via `onSuccess()` or `onFailure()`. The `MediationInterstitialAd` must implement a `showAd()` method. The `MediationInterstitialAdCallback` forwards ad events to the Google Mobile Ads SDK.\n"],null,["Prerequisites\n\nComplete the [custom events setup](/ad-manager/mobile-ads-sdk/android/custom-events/setup).\n\nRequest an interstitial ad\n\nWhen the custom event line item is reached in the waterfall mediation chain,\nthe `loadInterstitialAd()` method is called on the class name you provided when\n[creating a custom\nevent](/ad-manager/mobile-ads-sdk/android/custom-events/setup#create). In this case,\nthat method is in `SampleCustomEvent`, which then calls the\n`loadInterstitialAd()` method in `SampleInterstitialCustomEventLoader`.\n\nTo request an interstitial ad, create or modify a class that extends `Adapter`\nto implement `loadInterstitialAd()`. Additionally, create a new class to\nimplement `MediationInterstitialAd`.\n\nIn our [custom event example](//github.com/googleads/googleads-mobile-android-mediation/blob/main/Example/customevent/src/main/java/com/google/ads/mediation/sample/customevent/SampleCustomEvent.java),\n`SampleCustomEvent` extends the `Adapter` class and then delegates to\n`SampleInterstitialCustomEventLoader`. \n\nJava \n\n```java\npackage com.google.ads.mediation.sample.customevent;\n\nimport com.google.android.gms.ads.mediation.Adapter;\nimport com.google.android.gms.ads.mediation.MediationAdConfiguration;\nimport com.google.android.gms.ads.mediation.MediationAdLoadCallback;\nimport com.google.android.gms.ads.mediation.MediationInterstitialAd;\nimport com.google.android.gms.ads.mediation.MediationInterstitialAdCallback;\n...\n\npublic class SampleCustomEvent extends Adapter {\n private SampleInterstitialCustomEventLoader interstitialLoader;\n @Override\n public void loadInterstitialAd(\n @NonNull MediationInterstitialAdConfiguration adConfiguration,\n @NonNull\n MediationAdLoadCallback\u003cMediationInterstitialAd, MediationInterstitialAdCallback\u003e\n callback) {\n interstitialLoader = new SampleInterstitialCustomEventLoader(adConfiguration, callback);\n interstitialLoader.loadAd();\n }\n}\n```\n\n`SampleInterstitialCustomEventLoader` is responsible for the following tasks:\n\n- Loading the interstitial ad and invoking a\n [`MediationAdLoadCallback`](/ad-manager/mobile-ads-sdk/android/reference/com/google/android/gms/ads/mediation/MediationAdLoadCallback)\n method once loading completes.\n\n- Implementing the `MediationInterstitialAd` interface.\n\n- Receiving and reporting ad event callbacks to Google Mobile Ads SDK.\n\nThe optional parameter defined in the Ad Manager UI is\nincluded in the ad configuration. The parameter can be accessed through\n`adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)`.\nThis parameter is typically an ad unit identifier that an ad network SDK\nrequires when instantiating an ad object. \n\nJava \n\n```java\npackage com.google.ads.mediation.sample.customevent;\n\nimport com.google.android.gms.ads.mediation.Adapter;\nimport com.google.android.gms.ads.mediation.MediationInterstitialAdConfiguration;\nimport com.google.android.gms.ads.mediation.MediationAdLoadCallback;\nimport com.google.android.gms.ads.mediation.MediationInterstitialAd;\nimport com.google.android.gms.ads.mediation.MediationInterstitialAdCallback;\n...\n\npublic class SampleInterstitialCustomEventLoader extends SampleAdListener\n implements MediationInterstitialAd {\n\n /** A sample third-party SDK interstitial ad. */\n private SampleInterstitial sampleInterstitialAd;\n\n /** Configuration for requesting the interstitial ad from the third-party network. */\n private final MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration;\n\n /** Callback for interstitial ad events. */\n private MediationInterstitialAdCallback interstitialAdCallback;\n\n /** Callback that fires on loading success or failure. */\n private final MediationAdLoadCallback\u003cMediationInterstitialAd, MediationInterstitialAdCallback\u003e\n mediationAdLoadCallback;\n\n /** Constructor. */\n public SampleInterstitialCustomEventLoader(\n @NonNull MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration,\n @NonNull MediationAdLoadCallback\u003cMediationInterstitialAd, MediationInterstitialAdCallback\u003e\n mediationAdLoadCallback) {\n this.mediationInterstitialAdConfiguration = mediationInterstitialAdConfiguration;\n this.mediationAdLoadCallback = mediationAdLoadCallback;\n }\n\n /** Loads the interstitial ad from the third-party ad network. */\n public void loadAd() {\n // All custom events have a server parameter named \"parameter\" that returns\n // back the parameter entered into the UI when defining the custom event.\n Log.i(\"InterstitialCustomEvent\", \"Begin loading interstitial ad.\");\n String serverParameter = mediationInterstitialAdConfiguration.getServerParameters().getString(\n MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD);\n Log.d(\"InterstitialCustomEvent\", \"Received server parameter.\");\n\n sampleInterstitialAd =\n new SampleInterstitial(mediationInterstitialAdConfiguration.getContext());\n sampleInterstitialAd.setAdUnit(serverParameter);\n\n // Implement a SampleAdListener and forward callbacks to mediation.\n sampleInterstitialAd.setAdListener(this);\n\n // Make an ad request.\n Log.i(\"InterstitialCustomEvent\", \"start fetching interstitial ad.\");\n sampleInterstitialAd.fetchAd(\n SampleCustomEvent.createSampleRequest(mediationInterstitialAdConfiguration));\n }\n\npublic SampleAdRequest createSampleRequest(\n MediationAdConfiguration mediationAdConfiguration) {\n SampleAdRequest request = new SampleAdRequest();\n request.setTestMode(mediationAdConfiguration.isTestRequest());\n request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet());\n return request;\n }\n}\n```\n\nDepending on whether the ad is successfully fetched or encounters an error, you\nwould call either\n[`onSuccess()`](/ad-manager/mobile-ads-sdk/android/reference/com/google/android/gms/ads/mediation/MediationAdLoadCallback#onSuccess(MediationAdT))\nor\n[`onFailure()`](/ad-manager/mobile-ads-sdk/android/reference/com/google/android/gms/ads/mediation/MediationAdLoadCallback#onFailure(com.google.android.gms.ads.AdError)).\n`onSuccess()` is called by passing in an instance of the class that implements\n`MediationInterstitialAd`.\n\nTypically, these methods are implemented inside callbacks from the\nthird-party SDK your adapter implements. For this example, the Sample SDK\nhas a `SampleAdListener` with relevant callbacks: \n\nJava \n\n```java\n@Override\npublic void onAdFetchSucceeded() {\n interstitialAdCallback = mediationAdLoadCallback.onSuccess(this);\n}\n\n@Override\npublic void onAdFetchFailed(SampleErrorCode errorCode) {\n mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));\n}\n```\n\n`MediationInterstitialAd` requires implementing a `showAd()` method to display\nthe ad: \n\nJava \n\n```java\n@Override\npublic void showAd(@NonNull Context context) {\n sampleInterstitialAd.show();\n}\n```\n\nForward mediation events to Google Mobile Ads SDK\n\nOnce `onSuccess()` is called, the returned `MediationInterstitialAdCallback`\nobject can then be used by the adapter to forward presentation events from the\nthird-party SDK to Google Mobile Ads SDK. The\n`SampleInterstitialCustomEventLoader` class extends the `SampleAdListener`\ninterface to forward callbacks from the sample ad network to the Google Mobile\nAds SDK.\n\nIt's important that your custom event forwards as many of these callbacks as\npossible, so that your app receives these equivalent events from the Google\nMobile Ads SDK. Here's an example of using callbacks: \n\nJava \n\n```java\n@Override\npublic void onAdFullScreen() {\n interstitialAdCallback.reportAdImpression();\n interstitialAdCallback.onAdOpened();\n}\n\n@Override\npublic void onAdClosed() {\n interstitialAdCallback.onAdClosed();\n}\n```\n\nThis completes the custom events implementation for interstitial ads. The full\nexample is available on\n[GitHub](//github.com/googleads/googleads-mobile-android-mediation/tree/master/Example/customevent/src/main/java/com/google/ads/mediation/sample/customevent).\nYou can use it with an ad network that is already supported or modify it to\ndisplay custom event interstitial ads."]]