获取用户可重置的广告 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。
在支持多个用户(包括访客用户在内)的 Android 设备上,您的应用可能会在同一设备上获得不同的广告 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权限的提供程序。 - 在设备上安装时间最长的提供程序。
- 按字母顺序最先显示的提供程序。