取得可由使用者重設的廣告 ID Android Jetpack。
為保護使用者隱私,所有 Android 應用程式都應採用可由使用者重設的 ID 做為最佳做法。這類 ID 是廣告 ID, 可將特定使用者用於廣告用途 (例如廣告) 個人化。
如要在執行應用程式的裝置上支援標準化廣告追蹤解決方案,您可以使用廣告 ID 程式庫。這個程式庫適用於搭載 Android 4.0 (API 級別 14) 以上版本的裝置,可定義與系統層級廣告供應商互動的介面。此介面可讓應用程式接收一致的廣告 ID 值。
廣告 ID 程式庫中包含的廣告供應商,也會定義開啟廣告供應商實作的設定畫面的標準意圖。這項設定畫面可讓使用者重設廣告 ID,並停用廣告個人化功能。
本指南說明如何使用廣告 ID 程式庫的用戶端模組 取得一致的廣告 ID,適用於個別裝置的使用者。本指南 隨後概述程式庫的架構
設定用戶端應用程式
與廣告 ID 程式庫的用戶端模組互動,您的應用程式 就可以擷取一致的廣告 ID,代表 才能與應用程式互動
廣告 ID 是以全域不重複值第 3 版表示 識別碼 (UUID) 格式或 等同 128 位元格式:
38400000-8cf0-11bd-b23e-10b96e40000d
廣告 ID 程式庫會視需要將傳回值正規化,以便提供 編號。
如要擷取可由使用者重設的應用程式廣告 ID,請完成 步驟如下:
呼叫
AdvertisingIdClient.isAdvertisingIdProviderAvailable()
。 如果這個方法傳回false
,應用程式應使用其他方式執行任何必要的廣告追蹤用途。呼叫
AdvertisingIdClient.getAdvertisingIdInfo()
。廣告 ID 程式庫會在 worker 執行緒上執行此方法,並使用 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 程式庫的結構。這個程式庫包含下列模組:
- 用戶端模組,這是應用程式中包含的精簡層。
- 裝置製造商提供的供應者模組。這個模組的實作項目必須定義設定 UI,讓使用者能 重設廣告 ID,並切換廣告追蹤偏好設定。
用戶端模組會與供應商模組通訊,以擷取廣告 廣告追蹤的 ID 和使用者偏好設定。
程式庫如何處理多個供應者
裝置或許可以在第一層支援多個系統層級的廣告供應商 如果廣告 ID 程式庫偵測到這種情況,就會確保應用程式一律從相同的供應商擷取資訊 (假設供應商仍可用)。這項程序可確保廣告 ID 一致。
如果可用的廣告供應商組合會隨時間變化,而您的應用程式 與其他廣告 ID 供應商互動,所有其他用戶端應用程式即啟動 並使用這個新的供應商應用程式的行為與 會發生這種問題。
廣告 ID 供應商程式庫會採用下列決定順序 將供應商排名:
- 向 Google 提出
androidx.ads.identifier.provider.HIGH_PRIORITY
權限。 - 長時間安裝在裝置上的供應商。
- 按字母順序排序的供應商。