设置
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助自定义事件,您可以为支持的广告联盟以外的广告联盟添加广告瀑布流中介。为此,您可以为要集成的广告联盟实现自定义事件适配器。
前提条件
您必须先将以下广告格式之一集成到您的应用中,然后才能创建自定义事件:
在界面中创建自定义事件
您必须先在 Ad Manager 界面中创建自定义事件。请参阅创建和管理收益组中的说明。
您需要提供以下信息:
- 类名称
实现自定义事件适配器的类的完全限定名称,例如,com.google.ads.mediation.sample.customevent.SampleCustomEvent
。我们建议的最佳做法是,为所有自定义事件广告格式使用同一适配器类。
- 标签
定义广告来源的唯一名称。
- 参数
传递给自定义事件适配器的可选字符串参数。
初始化适配器
Google 移动广告 SDK 初始化时,系统会针对在 Ad Manager 界面中为应用配置的所有支持的第三方适配器和自定义事件调用 initialize()
。您可以使用此方法对自定义事件所需的第三方 SDK 执行任何必要的设置或初始化操作。
Java
package com.google.ads.mediation.sample.customevent;
import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.InitializationCompleteCallback;
import com.google.android.gms.ads.mediation.MediationConfiguration;
public class SampleAdNetworkCustomEvent extends Adapter {
private static final String SAMPLE_AD_UNIT_KEY = "parameter";
@Override
public void initialize(Context context,
InitializationCompleteCallback initializationCompleteCallback,
List<MediationConfiguration> mediationConfigurations) {
// This is where you will initialize the SDK that this custom
// event is built for. Upon finishing the SDK initialization,
// call the completion handler with success.
initializationCompleteCallback.onInitializationSucceeded();
}
}
Kotlin
package com.google.ads.mediation.sample.customevent
import com.google.android.gms.ads.mediation.Adapter
import com.google.android.gms.ads.mediation.InitializationCompleteCallback
import com.google.android.gms.ads.mediation.MediationConfiguration
class SampleCustomEvent : Adapter() {
private val SAMPLE_AD_UNIT_KEY = "parameter"
override fun initialize(
context: Context,
initializationCompleteCallback: InitializationCompleteCallback,
mediationConfigurations: List<MediationConfiguration>
) {
// This is where you will initialize the SDK that this custom
// event is built for. Upon finishing the SDK initialization,
// call the completion handler with success.
initializationCompleteCallback.onInitializationSucceeded()
}
}
报告版本号
所有自定义事件都必须向 Google 移动广告 SDK 报告自定义事件适配器本身的版本,以及与该自定义事件对接的第三方 SDK 的版本。报告版本时使用的是 VersionInfo
对象:
Java
package com.google.ads.mediation.sample.customevent;
public class SampleCustomEvent extends Adapter {
@Override
public VersionInfo getVersionInfo() {
String versionString = new VersionInfo(1, 2, 3);
String[] splits = versionString.split("\\.");
if (splits.length >= 4) {
int major = Integer.parseInt(splits[0]);
int minor = Integer.parseInt(splits[1]);
int micro = Integer.parseInt(splits[2]) * 100 + Integer.parseInt(splits[3]);
return new VersionInfo(major, minor, micro);
}
return new VersionInfo(0, 0, 0);
}
@Override
public VersionInfo getSDKVersionInfo() {
String versionString = SampleAdRequest.getSDKVersion();
String[] splits = versionString.split("\\.");
if (splits.length >= 3) {
int major = Integer.parseInt(splits[0]);
int minor = Integer.parseInt(splits[1]);
int micro = Integer.parseInt(splits[2]);
return new VersionInfo(major, minor, micro);
}
return new VersionInfo(0, 0, 0);
}
}
Kotlin
package com.google.ads.mediation.sample.customevent
class SampleCustomEvent : Adapter() {
override fun getVersionInfo(): VersionInfo {
val versionString = VersionInfo(1,2,3).toString()
val splits: List<String> = versionString.split("\\.")
if (splits.count() >= 4) {
val major = splits[0].toInt()
val minor = splits[1].toInt()
val micro = (splits[2].toInt() * 100) + splits[3].toInt()
return VersionInfo(major, minor, micro)
}
return VersionInfo(0, 0, 0)
}
override fun getSDKVersionInfo(): VersionInfo {
val versionString = VersionInfo(1,2,3).toString()
val splits: List<String> = versionString.split("\\.")
if (splits.count() >= 3) {
val major = splits[0].toInt()
val minor = splits[1].toInt()
val micro = splits[2].toInt()
return VersionInfo(major, minor, micro)
}
return VersionInfo(0, 0, 0)
}
}
请求广告
若要请求广告,请参阅适用于具体广告格式的操作说明:
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eCustom events enable waterfall mediation for unsupported ad networks by implementing a custom event adapter.\u003c/p\u003e\n"],["\u003cp\u003eBefore creating custom events, you must integrate a supported ad format (Banner, Interstitial, Native, or Rewarded) into your app.\u003c/p\u003e\n"],["\u003cp\u003eA custom event needs to be defined in the Ad Manager UI with a class name, label, and optional parameter.\u003c/p\u003e\n"],["\u003cp\u003eCustom event adapters must report their version and the third-party SDK version to the Google Mobile Ads SDK using \u003ccode\u003eVersionInfo\u003c/code\u003e objects.\u003c/p\u003e\n"],["\u003cp\u003eAd requests are handled differently for each ad format; refer to the specific instructions for Banner, Interstitial, Native, or Rewarded ads.\u003c/p\u003e\n"]]],[],null,["Custom events let you add waterfall mediation for an ad network that isn't a\n[supported ad network](/ad-manager/mobile-ads-sdk/android/choose-networks). You do this by implementing\na custom event adapter for the ad network you want to integrate.\n\nPrerequisites\n\nBefore you can create custom events, you must first integrate one of the\nfollowing ad format into your app:\n\n- [Banner](/ad-manager/mobile-ads-sdk/android/banner)\n- [Interstitial](/ad-manager/mobile-ads-sdk/android/interstitial)\n- [Native](/ad-manager/mobile-ads-sdk/android/native)\n- [Rewarded](/ad-manager/mobile-ads-sdk/android/rewarded)\n\nCreate a custom event in the UI\n\nA custom event must first be created in the Ad Manager\nUI. See the instructions in\n\n[Create and manage yield\ngroups](//support.google.com/admanager/answer/7390828).\n\n\nYou need to supply the following:\n\nClass Name\n\n: The fully-qualified name of the class that implements the custom event\n adapter---for example,\n\n `com.google.ads.mediation.sample.customevent.SampleCustomEvent`. As a best\n practice, we recommend using a single adapter class for all custom event ad\n formats.\n\nLabel\n\n: A unique name defining the ad source.\n\nParameter\n\n: An optional string argument passed to your custom event adapter.\n\nInitialize the adapter\n\nWhen Google Mobile Ads SDK initializes,\n\n`initialize()`\n\nis invoked on all supported third-party adapters and custom events configured\nfor the app within the Ad Manager UI. Use this method to\nperform any necessary setup or initialization on the required third-party SDK\nfor your custom event. \n\nJava \n\n package com.google.ads.mediation.sample.customevent;\n\n import com.google.android.gms.ads.mediation.Adapter;\n import com.google.android.gms.ads.mediation.InitializationCompleteCallback;\n import com.google.android.gms.ads.mediation.MediationConfiguration;\n\n public class SampleAdNetworkCustomEvent extends Adapter {\n private static final String SAMPLE_AD_UNIT_KEY = \"parameter\";\n\n @Override\n public void initialize(Context context,\n InitializationCompleteCallback initializationCompleteCallback,\n List\u003cMediationConfiguration\u003e mediationConfigurations) {\n // This is where you will initialize the SDK that this custom\n // event is built for. Upon finishing the SDK initialization,\n // call the completion handler with success.\n initializationCompleteCallback.onInitializationSucceeded();\n }\n }\n\nKotlin \n\n package com.google.ads.mediation.sample.customevent\n\n import com.google.android.gms.ads.mediation.Adapter\n import com.google.android.gms.ads.mediation.InitializationCompleteCallback\n import com.google.android.gms.ads.mediation.MediationConfiguration\n\n class SampleCustomEvent : Adapter() {\n private val SAMPLE_AD_UNIT_KEY = \"parameter\"\n\n override fun initialize(\n context: Context,\n initializationCompleteCallback: InitializationCompleteCallback,\n mediationConfigurations: List\u003cMediationConfiguration\u003e\n ) {\n // This is where you will initialize the SDK that this custom\n // event is built for. Upon finishing the SDK initialization,\n // call the completion handler with success.\n initializationCompleteCallback.onInitializationSucceeded()\n }\n }\n\nReport version numbers\n\nAll custom events must report to Google Mobile Ads SDK both the version of\nthe custom event adapter itself and the version of the third-party SDK the\ncustom event interfaces with. Versions are reported as\n\n[`VersionInfo`](/ad-manager/mobile-ads-sdk/android/reference/com/google/android/gms/ads/VersionInfo)\n\nobjects: \n\nJava \n\n package com.google.ads.mediation.sample.customevent;\n\n public class SampleCustomEvent extends Adapter {\n\n @Override\n public VersionInfo getVersionInfo() {\n String versionString = new VersionInfo(1, 2, 3);\n String[] splits = versionString.split(\"\\\\.\");\n\n if (splits.length \u003e= 4) {\n int major = Integer.parseInt(splits[0]);\n int minor = Integer.parseInt(splits[1]);\n int micro = Integer.parseInt(splits[2]) * 100 + Integer.parseInt(splits[3]);\n return new VersionInfo(major, minor, micro);\n }\n\n return new VersionInfo(0, 0, 0);\n }\n\n @Override\n public VersionInfo getSDKVersionInfo() {\n String versionString = SampleAdRequest.getSDKVersion();\n String[] splits = versionString.split(\"\\\\.\");\n\n if (splits.length \u003e= 3) {\n int major = Integer.parseInt(splits[0]);\n int minor = Integer.parseInt(splits[1]);\n int micro = Integer.parseInt(splits[2]);\n return new VersionInfo(major, minor, micro);\n }\n\n return new VersionInfo(0, 0, 0);\n }\n }\n\nKotlin \n\n package com.google.ads.mediation.sample.customevent\n\n class SampleCustomEvent : Adapter() {\n override fun getVersionInfo(): VersionInfo {\n val versionString = VersionInfo(1,2,3).toString()\n val splits: List\u003cString\u003e = versionString.split(\"\\\\.\")\n\n if (splits.count() \u003e= 4) {\n val major = splits[0].toInt()\n val minor = splits[1].toInt()\n val micro = (splits[2].toInt() * 100) + splits[3].toInt()\n return VersionInfo(major, minor, micro)\n }\n\n return VersionInfo(0, 0, 0)\n }\n\n override fun getSDKVersionInfo(): VersionInfo {\n val versionString = VersionInfo(1,2,3).toString()\n val splits: List\u003cString\u003e = versionString.split(\"\\\\.\")\n\n if (splits.count() \u003e= 3) {\n val major = splits[0].toInt()\n val minor = splits[1].toInt()\n val micro = splits[2].toInt()\n return VersionInfo(major, minor, micro)\n }\n\n return VersionInfo(0, 0, 0)\n }\n }\n\nRequest ad\n\nTo request an ad, refer to the instructions specific to the ad format:\n\n- [Banner](/ad-manager/mobile-ads-sdk/android/custom-events/banner)\n- [Interstitial](/ad-manager/mobile-ads-sdk/android/custom-events/interstitial)\n- [Native](/ad-manager/mobile-ads-sdk/android/custom-events/native)\n- [Rewarded](/ad-manager/mobile-ads-sdk/android/custom-events/rewarded)"]]