获取用户可重置的广告 ID Android Jetpack 的一部分。
为了保护用户隐私,所有 Android 应用都应该使用用户可重置的标识符。例如,广告 ID 就是这样的一种标识符,它唯一地标识广告用例(例如广告个性化)的特定用户。
如需在运行您的应用的不同设备上支持标准化的广告跟踪解决方案,您可以使用广告 ID 库。该库可在搭载 Android 4.0(API 级别 14)及更高版本的设备上使用,它定义了与系统级广告提供程序进行交互的接口。此接口可让您的应用接收一致的广告 ID 值。
广告 ID 库随附的广告提供程序还定义了一个标准 intent,用于打开广告提供程序实现的设置屏幕。用户可以在此设置界面中重置其广告 ID,以及选择停用广告个性化功能。
本指南介绍了如何使用广告 ID 库的客户端模块为每个设备用户获得一致的广告 ID。然后简要说明该库的架构。
配置客户端应用
通过与广告 ID 库的客户端模块交互,您的应用可以检索一致的广告 ID,该 ID 表示与应用互动的用户。
广告 ID 使用通用唯一标识符 (UUID) 格式(版本 3)或等效的 128 位格式表示:
38400000-8cf0-11bd-b23e-10b96e40000d
广告 ID 库会根据需要标准化返回值,以使用此格式来提供 ID。
如需为应用检索用户可重置的广告 ID,请完成以下步骤:
- 通过调用 - AdvertisingIdClient.isAdvertisingIdProviderAvailable()检查是否有可用的广告提供程序。如果此方法返回- false,则您的应用必须使用其他方式来执行任何必要的广告跟踪用例。
- 通过调用 - AdvertisingIdClient.getAdvertisingIdInfo()获得广告标识符的详细信息,包括广告 ID。广告 ID 库在工作线程上执行此方法,并使用 10 秒连接超时。
以下代码段展示了如何检索广告 ID 以及来自广告提供程序的其他信息:
app/build.gradle
Groovy
dependencies { implementation 'androidx.ads:ads-identifier:1.0.0-alpha01' // Used for the calls to addCallback() in the snippets on this page. implementation 'com.google.guava:guava:28.0-android' }
Kotlin
dependencies { implementation("androidx.ads:ads-identifier:1.0.0-alpha01") // Used for the calls to addCallback() in the snippets on this page. implementation("com.google.guava:guava:28.0-android") }
MyAdIdClient
Kotlin
// Used for the call to addCallback() within this snippet.
import com.google.common.util.concurrent.Futures.addCallback
private fun determineAdvertisingInfo() {
    if (AdvertisingIdClient.isAdvertisingIdProviderAvailable()) {
        val advertisingIdInfoListenableFuture =
                AdvertisingIdClient.getAdvertisingIdInfo(applicationContext)
        addCallback(advertisingIdInfoListenableFuture,
                object : FutureCallback<AdvertisingIdInfo> {
            override fun onSuccess(adInfo: AdvertisingIdInfo?) {
                val id: String = adInfo?.id
                val providerPackageName: String = adInfo?.providerPackageName
                val isLimitTrackingEnabled: Boolean =
                                adInfo?.isLimitTrackingEnabled
            }
            // Any exceptions thrown by getAdvertisingIdInfo()
            // cause this method to be called.
            override fun onFailure(t: Throwable) {
                Log.e("MY_APP_TAG",
                        "Failed to connect to Advertising ID provider.")
                // Try to connect to the Advertising ID provider again or fall
                // back to an ad solution that doesn't require using the
                // Advertising ID library.
            }
        }, Executors.newSingleThreadExecutor())
    } else {
        // The Advertising ID client library is unavailable. Use a different
        // library to perform any required ad use cases.
    }
}
Java
// Used for the call to addCallback() within this snippet.
import com.google.common.util.concurrent.Futures;
private void determineAdvertisingInfo() {
    if (AdvertisingIdClient.isAdvertisingIdProviderAvailable()) {
        ListenableFuture<AdvertisingIdInfo> advertisingIdInfoListenableFuture =
                AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
        Futures.addCallback(advertisingIdInfoListenableFuture,
                new FutureCallback<AdvertisingIdInfo>() {
                    @Override
                    public void onSuccess(AdvertisingIdInfo adInfo) {
                        String id = adInfo.getId();
                        String providerPackageName =
                                adInfo.getProviderPackageName();
                        boolean isLimitTrackingEnabled =
                                adInfo.isLimitTrackingEnabled();
                    // Any exceptions thrown by getAdvertisingIdInfo()
                    // cause this method to be called.
                    @Override
                    public void onFailure(Throwable throwable) {
                        Log.e("MY_APP_TAG",
                                "Failed to connect to Advertising ID provider.");
                        // Try to connect to the Advertising ID provider again
                        // or fall back to an ad solution that doesn't require
                        // using the Advertising ID library.
                    }
                });
    } else {
        // The Advertising ID client library is unavailable. Use a different
        // library to perform any required ad use cases.
    }
}
广告 ID 库架构
图 1 展示了广告 ID 库的结构。该库包含以下模块:
- 客户端模块,它是应用中包含的一个薄层。
- 提供程序模块,由设备制造商提供。此模块的实现必须定义设置界面,供用户重置其广告 ID 和切换广告跟踪偏好设置。
客户端模块与提供程序模块进行通信,以检索广告 ID 和有关广告跟踪的用户偏好设置。
广告 ID 库如何处理多个提供程序
一台设备可以同时支持多个系统级的广告提供程序。如果广告 ID 库检测到这种情况,它会验证您的应用是否始终从同一个提供程序检索信息,但前提是该提供程序保持可用。此过程可让广告 ID 保持一致。
如果可用的广告提供程序随着时间发生变化,使得您的应用与其他广告标识符提供程序进行交互,则所有其他客户端应用也会开始使用这一新的提供程序。您的应用将表现出与用户请求重置其广告 ID 时相同的行为。
广告 ID 提供程序库使用以下确定顺序对提供程序进行排序:
- 已请求 androidx.ads.identifier.provider.HIGH_PRIORITY权限的提供程序。
- 在设备上安装时间最长的提供程序。
- 按字母顺序最先显示的提供程序。
