获取用户可重置的广告 ID Android Jetpack 的一部分。
为保护用户隐私,所有 Android 应用的最佳实践是使用用户可重置的标识符。其中一个标识符是 advertising ID,它能唯一标识特定用户,用于广告用例,例如广告个性化。
为了在运行您应用的设备上支持标准化的广告跟踪解决方案,您可以使用 Advertising ID library。此库适用于运行 Android 4.0 (API 级别 14) 及更高版本的设备,它定义了一个接口,用于与系统级广告提供方交互。此接口可让您的应用接收一致的广告 ID 值。
Advertising ID library 中包含的广告提供方还定义了一个标准 intent,用于打开广告提供方实现的设置屏幕。此设置屏幕允许用户重置其广告 ID 并选择停用广告个性化。
本指南介绍了如何使用 Advertising ID library 的客户端模块,以便在每个设备用户的基础上获取一致的广告 ID。本指南随后概述了该库的架构。
配置您的客户端应用
通过与 Advertising ID library 的客户端模块交互,您的应用可以检索到代表正在与应用交互的用户的、一致的广告 ID。
advertising ID 使用版本 3 的 通用唯一标识符 (UUID) 格式或等效的 128 位格式表示
38400000-8cf0-11bd-b23e-10b96e40000d
Advertising ID library 会根据需要对返回值进行规范化处理,以便使用此格式提供 ID。
要检索应用的用户可重置广告 ID,请完成以下步骤
通过调用
AdvertisingIdClient.isAdvertisingIdProviderAvailable()
检查广告提供方是否可用。如果此方法返回false
,您的应用应使用其他方式执行所需的广告跟踪用例。通过调用
AdvertisingIdClient.getAdvertisingIdInfo()
获取广告标识符详细信息,包括 advertising ID。Advertising ID library 在工作线程上执行此方法,并使用 10 秒的连接超时。
以下代码段演示了如何从广告提供方获取 advertising 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. } }
Advertising ID library 架构
图 1 描述了 Advertising ID library 的结构。该库包含以下模块:
- 客户端模块:包含在应用中的一个薄层。
- 提供方模块:由设备制造商提供。此模块的实现必须定义一个设置界面,让用户能够重置其 advertising ID 和切换广告跟踪偏好设置。
客户端模块与提供方模块通信,以检索 advertising ID 和用户关于广告跟踪的偏好设置。
该库如何处理多个提供方
设备可能同时支持多个系统级广告提供方。如果 Advertising ID library 检测到这种情况,它会确保您的应用始终从同一提供方检索信息,前提是该提供方保持可用。此过程可保持 advertising ID 的一致性。
如果可用广告提供方的集合随时间变化,并且您的应用与不同的广告标识符提供方交互,则所有其他客户端应用也会开始使用该新提供方。您的应用会表现出与用户请求重置其 advertising ID 时相同的行为。
Advertising ID 提供方库使用以下确定性顺序对提供方进行排名:
- 已请求
androidx.ads.identifier.provider.HIGH_PRIORITY
权限的提供方。 - 在设备上安装时间最长的提供方。
- 按字母顺序排在最前面的提供方。