开始使用

本指南介绍了如何将中介适配器与 Android 应用集成。

前提条件

在为广告格式集成中介功能之前,您需要先将该广告格式集成到您的应用中:

第一次使用中介功能?阅读中介简介

对于出价:Google 移动广告 SDK 18.3.0 或更高版本。

初始化移动广告 SDK

快速入门指南介绍了如何初始化移动广告 SDK。在初始化调用期间,中介和出价适配器也会初始化。请务必在初始化完成后再加载广告,以确保每个广告联盟完全参与第一个广告请求。

以下示例代码展示了如何在发出广告请求之前检查每个适配器的初始化状态。

Java

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.AdapterStatus;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(
                () ->
                    // Initialize the Google Mobile Ads SDK on a background thread.
                    MobileAds.initialize(
                        this,
                        initializationStatus -> {
                          Map<String, AdapterStatus> statusMap =
                              initializationStatus.getAdapterStatusMap();
                          for (String adapterClass : statusMap.keySet()) {
                            AdapterStatus status = statusMap.get(adapterClass);
                            Log.d(
                                "MyApp",
                                String.format(
                                    "Adapter name: %s, Description: %s, Latency: %d",
                                    adapterClass, status.getDescription(), status.getLatency()));
                          }
                          // Start loading ads here...
                        }))
            .start();
    }
}

Kotlin

import com.google.android.gms.ads.MobileAds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val backgroundScope = CoroutineScope(Dispatchers.IO)
    backgroundScope.launch {
      // Initialize the Google Mobile Ads SDK on a background thread.
      MobileAds.initialize(this@MainActivity) { initializationStatus ->
        val statusMap =
          initializationStatus.adapterStatusMap
        for (adapterClass in statusMap.keys) {
          val status = statusMap[adapterClass]
          Log.d(
            "MyApp", String.format(
              "Adapter name: %s, Description: %s, Latency: %d",
              adapterClass, status!!.description, status.latency
            )
          )
        }
        // Start loading ads here...
      }
    }
  }
}

检查哪个广告联盟适配器类加载了广告

以下是一些示例代码,用于记录横幅广告的广告网络类名称:

Java

public void onAdLoaded() {
  Log.d("Banner adapter class name: " + ad.getResponseInfo().getMediationAdapterClassName());
}

Kotlin

override fun onAdLoaded() {
  Log.d("Banner adapter class name:" + ad.responseInfo.mediationAdapterClassName)
}

如需详细了解此方法,请参阅关于 getMediationAdapterClassName()ResponseInfo 文档。

使用 Activity 实例初始化广告对象

在新广告对象(例如 AdManagerAdView)的构造函数中,您必须传入 Context 类型的对象。使用中介时,此 Context 会传递到其他广告联盟。某些广告联盟需要类型为 Activity 且限制更严格的 Context,并且如果没有 Activity 实例,可能无法投放广告。因此,我们建议您在初始化广告对象时传入 Activity 实例,以确保从您的所有中介广告联盟获得一致的体验。

对于 Ad Manager 中介中使用的横幅广告单元,请确保停用所有第三方广告联盟界面中的刷新功能。这可防止重复刷新,因为 Ad Manager 还会根据横幅广告单元的刷新频率触发刷新。

原生广告中介

以下是实现原生中介时需要考虑的一些最佳做法。

原生广告展示政策
每个广告网络都有自己的政策。使用中介功能时请务必注意,您的应用仍需遵守提供相应广告的中介广告联盟的政策。
使用 loadAd(),而不要使用 loadAds()
loadAds() 方法仅投放 Google 广告。对于参与中介的广告,请改用 loadAd()

美国州级隐私保护法律和 GDPR

如果您需要遵守美国州级隐私保护法律一般数据保护条例 (GDPR),请按照美国州级法规设置GDPR 设置中的步骤,在 Ad Manager 的“隐私权和消息”工具的美国各州或 GDPR 广告合作伙伴列表中添加中介合作伙伴。否则,可能会导致合作伙伴无法在您的应用中投放广告。

详细了解如何使用 Google User Messaging Platform (UMP) SDK 启用受限的数据处理 (RDP) 并获取 GDPR 用户同意。