呼叫 onSuccess() 後,轉接程式可使用傳回的 MediationInterstitialAdCallback 物件,將呈現事件從第三方 SDK 轉送至 Google Mobile Ads SDK。SampleInterstitialCustomEventLoader 類別可延伸 SampleAdListener 介面,將回呼從範例廣告聯播網轉送到 Google Mobile Ads SDK。
自訂事件應盡量將這些回呼轉送至 Google Mobile Ads SDK,這樣應用程式才能收到相應的事件通知。以下是回呼使用範例:
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-05 (世界標準時間)。"],[[["\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."]]