AdvertisingIdClient.Info | Google Play services API 参考 原文标题:AdvertisingIdClient.Info | Google Play services | Google for Developers
内容概要总结
Google Play services 中 AdvertisingIdClient.Info 类的官方 API 参考。该类同时包含广告标识符(advertising ID,即 GAID)以及「限制广告追踪」(limit ad tracking)设置。
getId() 方法获取广告标识符:当用户启用了限制广告追踪,或未声明 AD_ID 权限时,返回的可能是全零字符串(00000000-0000-0000-0000-000000000000)。isLimitAdTrackingEnabled() 检查用户是否启用了限制广告追踪。构造函数已废弃,用户无需通过它访问信息。
文档给出重要变更时间线:从 2021 年底起,在 Android 12 设备上,当 isLimitAdTrackingEnabled() 为 true 时,getId() 返回全零值,与 App 的 target SDK 级别无关;2022 年初起该变更适用于所有支持 Google Play services 的设备。target API level 为 33(Android 13)或更高的 App 必须在 AndroidManifest.xml 中声明 normal 权限 com.google.android.gms.permission.AD_ID 才能使用该 API,安装时即授予;未声明时自 2022 年初起返回全零。target API level 为 32(Android 12L)或更旧的 App 不需要该权限。文档还给出用 tools:node="remove" 阻止该权限经 SDK 依赖合并进 App 的方法。
翻译内容
原文内容(English)
public static final class AdvertisingIdClient.Info extends Object
同时包含广告标识符(advertising ID)以及限制广告追踪(limit ad tracking)设置。
页面摘要
AdvertisingIdClient.Info 同时包含广告标识符和限制广告追踪设置。
getId() 方法获取广告标识符;如果启用了限制广告追踪,或未声明 AD_ID 权限,它可能是一串零。
isLimitAdTrackingEnabled() 方法检查用户是否启用了限制广告追踪。
AdvertisingIdClient.Info 的构造函数已废弃,用户无需借助它来访问这些信息。
公开构造函数摘要
Info(String advertisingId, boolean limitAdTrackingEnabled)—— 此构造函数已废弃。 用户无需通过此构造函数来访问该信息。
公开方法摘要
| 返回类型 | 方法 | 说明 |
|---|---|---|
| String | getId() | 获取广告标识符。 |
| boolean | isLimitAdTrackingEnabled() | 获取用户是否启用了限制广告追踪。 |
| String | toString() | 继承自 Object。 |
继承的方法摘要
来自 java.lang.Object 类。
公开构造函数
public Info (String advertisingId, boolean limitAdTrackingEnabled)
此构造函数已废弃。 用户无需通过此构造函数来访问该信息。
使用指定的广告标识符和限制广告追踪设置构造一个 Info 对象。
参数
advertisingId—— 广告标识符。limitAdTrackingEnabled—— 限制广告追踪设置。如果用户启用了限制广告追踪则为 true,否则为 false。
公开方法
public String getId ()
获取广告标识符。
从 2021 年底起,在 Android 12 设备上,当 isLimitAdTrackingEnabled() 为 true 时,此 API 的返回值将是 00000000-0000-0000-0000-000000000000,无论 App 的 target SDK 级别为何。
2022 年初,此变更将应用于所有支持 Google Play services 的设备。
target API level 设置为 33(Android 13)或更高的 App,必须在 AndroidManifest.xml 中如下声明 normal 权限 com.google.android.gms.permission.AD_ID,才能使用此 API。
该权限会在 App 安装时被授予。
如果未声明该权限,从 2022 年初起返回值将是 00000000-0000-0000-0000-000000000000。
在此之前,为帮助开发者,当 App 的 target API level 为 33(Android 13)或更高且缺少该权限时,会记录一行警告。该警告行位于 Log 标签 AdvertisingIdSettings 之下。
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
如果你需要阻止该权限通过 SDK 等依赖被合并进你的 App,请改为在你的 manifest 中包含以下元素:
<uses-permission android:name="com.google.android.gms.permission.AD_ID"
tools:node="remove"/>
对于 target API level 设置为 32(Android 12L)或更旧的 App,不需要该权限。
更多详情请参阅 Advertising ID Play Console Help。
public boolean isLimitAdTrackingEnabled ()
获取用户是否启用了限制广告追踪。
当返回值为 true 时,自 Android 12 起 getId() 的返回值将始终为 00000000-0000-0000-0000-000000000000。
public String toString ()
除另有说明外,本页内容依据 Creative Commons Attribution 4.0 许可授权,代码示例依据 Apache 2.0 许可授权。详情请参阅 Google Developers Site Policies。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新于 2026-01-27 UTC。
AdvertisingIdClient.Info
Stay organized with collections
Save and categorize content based on your preferences.
Page Summary
AdvertisingIdClient.Info includes both the advertising ID and the limit ad tracking setting.
The getId() method retrieves the advertising ID, which may be a string of zeros if limit ad tracking is enabled or the AD_ID permission is not declared.
The isLimitAdTrackingEnabled() method checks if the user has enabled limit ad tracking.
The constructor for AdvertisingIdClient.Info is deprecated and not necessary for users to access the information.
public static final class AdvertisingIdClient.Info extends Object
Includes both the advertising ID as well as the limit ad tracking setting.
Public Constructor Summary
Info(String
advertisingId, boolean limitAdTrackingEnabled)
This constructor is deprecated. This constructor is not necessary for the
users to access the info.
Public Method Summary
String
Retrieves the advertising ID.
boolean
isLimitAdTrackingEnabled()
Retrieves whether the user has limit ad tracking enabled or not.
String
Inherited Method Summary
From class java.lang.Object
Object
boolean
void
final Class<?>
int
final void
final void
String
final void
wait(long arg0, int arg1)
final void
final void
Public Constructors
public Info (String advertisingId, boolean
limitAdTrackingEnabled)
This constructor is deprecated.
This constructor is not necessary for the users to access the info.
Constructs an Info Object with the specified advertising ID and limit
ad tracking setting.
Parameters
| advertisingId |
| --- |
| The advertising ID. |
| limitAdTrackingEnabled |
| --- |
| The limit ad tracking setting. It is true if the user has limit ad tracking enabled. False, otherwise. |
Public Methods
public String getId ()
Retrieves the advertising ID.
Starting from late 2021, on Android 12 devices, when
isLimitAdTrackingEnabled() is true, the returned value of this
API will be 00000000-0000-0000-0000-000000000000 regardless of the app’s
target SDK level.
In early 2022, this change will be applied to all the devices that support Google
Play services.
Apps with target API level set to 33 (Android 13) or later must declare the normal
permission com.google.android.gms.permission.AD_ID as below in the
AndroidManifest.xml in order to use this API.
- This permission will be granted when the app is installed.
- If this permission is not declared, the returned value will be 00000000-0000-0000-0000-000000000000 starting early 2022.
- Until then, to help developers, a warning line is logged if the permission is missing when the app targets API level 33 (Android 13) or higher. This warning line is under the Log tag AdvertisingIdSettings.
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
If you need to prevent the permission from getting merged into your
app through dependencies such as SDKs, include the below element in your manifest
instead.
<uses-permission android:name="com.google.android.gms.permission.AD_ID"
tools:node="remove"/>
For apps with target API level set to 32 (Android 12L) or older, this permission is
not needed.
See Advertising ID Play
Console Help for more details.
public boolean isLimitAdTrackingEnabled ()
Retrieves whether the user has limit ad tracking enabled or not.
When the returned value is true, the returned value of
getId() will always be 00000000-0000-0000-0000-000000000000
starting with Android 12.
public String toString ()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-01-27 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-01-27 UTC."],[],["The AdvertisingIdClient.Info class provides access to the advertising ID and the user's limit ad tracking preference. Key actions include retrieving the advertising ID via getId(), which returns a string, and checking if ad tracking is limited using isLimitAdTrackingEnabled(), which returns a boolean. Starting with Android 12, when limit ad tracking is enabled, getId() returns all zeroes. Apps targeting Android 13 or later require the com.google.android.gms.permission.AD_ID permission to access the ID.\n"]]