Play Install Referrer Library
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用 Google Play 商店的 Install Referrer API,即可從 Google Play 中安全地擷取參照內容。Play Install Referrer API 用戶端程式庫採用 Java 程式設計語言編寫,是 Android 介面定義語言 (AIDL) 檔案的包裝函式,可定義 Install Referrer 服務的介面。您可以使用 Play Install Referrer API 用戶端程式庫來簡化開發流程。
本指南說明使用 Play Install Referrer 程式庫從 Google Play 擷取參照資訊的基礎知識。
更新應用程式的依附元件
請在應用程式的 build.gradle
檔案的依附元件區段中新增以下這行:
Groovy
dependencies {
...
implementation "com.android.installreferrer:installreferrer:2.2"
}
Kotlin
dependencies {
...
implementation("com.android.installreferrer:installreferrer:2.2")
}
連結至 Google Play
您必須先按照下列步驟與 Play 商店應用程式建立連線,才能使用 Play Install Referrer API 程式庫:
- 呼叫
newBuilder()
方法來建立 InstallReferrerClient
類別的執行個體。
呼叫 startConnection()
建立與 Google Play 的連線。
startConnection()
是非同步方法,因此您必須覆寫
InstallReferrerStateListener
。
之後才接收回呼
startConnection()
完成。
覆寫 onInstallReferrerSetupFinished()
方法,以便在回呼完成時收到通知。您須使用回應代碼呼叫此方法來處理不同的狀態。OK
表示連線成功。其他的 InstallReferrerResponse
常數用於不同的錯誤類型。
覆寫 onInstallReferrerServiceDisconnected()
方法以處理與 Google Play 連線中斷的問題。例如,如果 Play 商店服務在背景進行更新,Play Install Referrer 程式庫用戶端可能會斷線。程式庫用戶端必須呼叫 startConnection()
方法重新啟動連線,才能再提出其他請求。
以下程式碼示範如何啟動並測試與 Play 商店應用程式的連線:
Kotlin
private lateinit var referrerClient: InstallReferrerClient
referrerClient = InstallReferrerClient.newBuilder(this).build()
referrerClient.startConnection(object : InstallReferrerStateListener {
override fun onInstallReferrerSetupFinished(responseCode: Int) {
when (responseCode) {
InstallReferrerResponse.OK -> {
// Connection established.
}
InstallReferrerResponse.FEATURE_NOT_SUPPORTED -> {
// API not available on the current Play Store app.
}
InstallReferrerResponse.SERVICE_UNAVAILABLE -> {
// Connection couldn't be established.
}
}
}
override fun onInstallReferrerServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
}
})
Java
InstallReferrerClient referrerClient;
referrerClient = InstallReferrerClient.newBuilder(this).build();
referrerClient.startConnection(new InstallReferrerStateListener() {
@Override
public void onInstallReferrerSetupFinished(int responseCode) {
switch (responseCode) {
case InstallReferrerResponse.OK:
// Connection established.
break;
case InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
// API not available on the current Play Store app.
break;
case InstallReferrerResponse.SERVICE_UNAVAILABLE:
// Connection couldn't be established.
break;
}
}
@Override
public void onInstallReferrerServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
}
});
取得安裝參照網址
與 Play 商店應用程式建立連線後,請完成下列步驟,以取得安裝參照網址的詳細資料:
使用已同步的 getInstallReferrer()
方法傳回 ReferrerDetails
的執行個體。
使用 ReferrerDetails
類別的方法取得安裝參照網址的詳細資料。
以下程式碼示範如何存取安裝參照網址的資訊:
Kotlin
val response: ReferrerDetails = referrerClient.installReferrer
val referrerUrl: String = response.installReferrer
val referrerClickTime: Long = response.referrerClickTimestampSeconds
val appInstallTime: Long = response.installBeginTimestampSeconds
val instantExperienceLaunched: Boolean = response.googlePlayInstantParam
Java
ReferrerDetails response = referrerClient.getInstallReferrer();
String referrerUrl = response.getInstallReferrer();
long referrerClickTime = response.getReferrerClickTimestampSeconds();
long appInstallTime = response.getInstallBeginTimestampSeconds();
boolean instantExperienceLaunched = response.getGooglePlayInstantParam();
注意:安裝參照網址資訊的有效期為 90 天,而且除非使用者重新安裝應用程式,否則不會改變。為避免應用程式中發出不必要的 API 呼叫,在安裝完成後第一次執行時,請叫用 API 一次即可。
關閉服務連線
取得參照資訊後,請在 InstallReferrerClient
執行個體上呼叫 endConnection()
方法以關閉連線。關閉連線有助於避免資料外洩和效能問題。
詳情請參閱「Play Install Referrer 程式庫參考資料」。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Play Install Referrer Library\n\nYou can use the Google Play Store's Install Referrer API to securely retrieve\nreferral content from Google Play. The Play Install Referrer API Client Library\nis written in the Java programming language and is a wrapper for the Android\nInterface Definition Language (AIDL) file that defines the interface to the\nInstall Referrer service. You can use the Play Install Referrer API Client\nLibrary to simplify your development process.\n\nThis guide covers the basics of retrieving referral information from Google\nPlay using the Play Install Referrer Library.\n| **Note:** If you are developing your app using a language other than the Kotlin programming language or the Java programming language, you can interact directly with the AIDL file by using the [Play Install Referrer\n| API](/google/play/installreferrer/igetinstallreferrerservice).\n\nUpdating your app's dependencies\n--------------------------------\n\nAdd the following line to the dependencies section of the `build.gradle` file\nfor your app: \n\n### Groovy\n\n```groovy\ndependencies {\n ...\n implementation \"com.android.installreferrer:installreferrer:2.2\"\n}\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n ...\n implementation(\"com.android.installreferrer:installreferrer:2.2\")\n}\n```\n\nConnecting to Google Play\n-------------------------\n\nBefore you can use the Play Install Referrer API Library, you must\nestablish a connection to the Play Store app using the following\nsteps:\n\n1. Call the [`newBuilder()`](/reference/com/android/installreferrer/api/InstallReferrerClient#newBuilder(android.content.Context)) method to create an instance of [`InstallReferrerClient`](/reference/com/android/installreferrer/api/InstallReferrerClient) class.\n2. Call the\n [`startConnection()`](/reference/com/android/installreferrer/api/InstallReferrerClient#startConnection(com.android.installreferrer.api.InstallReferrerStateListener))\n to establish a connection to Google Play.\n\n3. The [`startConnection()`](/reference/com/android/installreferrer/api/InstallReferrerClient#startConnection(com.android.installreferrer.api.InstallReferrerStateListener))\n method is asynchronous, so you must override\n [`InstallReferrerStateListener`](/reference/com/android/installreferrer/api/InstallReferrerStateListener)\n to receive a callback after\n [`startConnection()`](/reference/com/android/installreferrer/api/InstallReferrerClient#startConnection(com.android.installreferrer.api.InstallReferrerStateListener))\n completes.\n\n4. Override the [`onInstallReferrerSetupFinished()`](/reference/com/android/installreferrer/api/InstallReferrerStateListener#onInstallReferrerSetupFinished(int))\n method to be notified when the callback completes. This method is called\n with a response code that you must use to handle the different states.\n [`OK`](https://developer.android.com/reference/com/android/installreferrer/api/InstallReferrerClient.InstallReferrerResponse#OK)\n indicates that the connection was successful. Each of the other\n [`InstallReferrerResponse`](/reference/com/android/installreferrer/api/InstallReferrerClient.InstallReferrerResponse)\n constants are for different types of errors.\n\n5. Override the [`onInstallReferrerServiceDisconnected()`](/reference/com/android/installreferrer/api/InstallReferrerStateListener#oninstallreferrerservicedisconnected)\n method to handle lost connections to Google Play. For\n example, the Play Install Referrer Library client may lose the connection if the\n Play Store service is updating in the background. The library client must call\n the\n [`startConnection()`](/reference/com/android/installreferrer/api/InstallReferrerClient#startConnection(com.android.installreferrer.api.InstallReferrerStateListener))\n method to restart the connection before making further requests.\n\nThe following code demonstrates how to start and test a connection to the\nPlay Store app: \n\n### Kotlin\n\n```kotlin\nprivate lateinit var referrerClient: InstallReferrerClient\n\nreferrerClient = InstallReferrerClient.newBuilder(this).build()\nreferrerClient.startConnection(object : InstallReferrerStateListener {\n\n override fun onInstallReferrerSetupFinished(responseCode: Int) {\n when (responseCode) {\n InstallReferrerResponse.OK -\u003e {\n // Connection established.\n }\n InstallReferrerResponse.FEATURE_NOT_SUPPORTED -\u003e {\n // API not available on the current Play Store app.\n }\n InstallReferrerResponse.SERVICE_UNAVAILABLE -\u003e {\n // Connection couldn't be established.\n }\n }\n }\n\n override fun onInstallReferrerServiceDisconnected() {\n // Try to restart the connection on the next request to\n // Google Play by calling the startConnection() method.\n }\n})\n```\n\n### Java\n\n```java\nInstallReferrerClient referrerClient;\n\nreferrerClient = InstallReferrerClient.newBuilder(this).build();\nreferrerClient.startConnection(new InstallReferrerStateListener() {\n @Override\n public void onInstallReferrerSetupFinished(int responseCode) {\n switch (responseCode) {\n case InstallReferrerResponse.OK:\n // Connection established.\n break;\n case InstallReferrerResponse.FEATURE_NOT_SUPPORTED:\n // API not available on the current Play Store app.\n break;\n case InstallReferrerResponse.SERVICE_UNAVAILABLE:\n // Connection couldn't be established.\n break;\n }\n }\n\n @Override\n public void onInstallReferrerServiceDisconnected() {\n // Try to restart the connection on the next request to\n // Google Play by calling the startConnection() method.\n }\n});\n```\n\nGetting the install referrer\n----------------------------\n\nAfter you have established a connection to the Play Store app, get the details\nfrom the install referrer by completing the following steps:\n\n1. Use the synchronized\n [`getInstallReferrer()`](/reference/com/android/installreferrer/api/InstallReferrerClient#getInstallReferrer())\n method to return an instance of\n [`ReferrerDetails`](/reference/com/android/installreferrer/api/ReferrerDetails).\n\n2. Use the methods that the\n [`ReferrerDetails`](/reference/com/android/installreferrer/api/ReferrerDetails)\n class provides to get details about the install referrer.\n\nThe following code demonstrates how you can access the install referrer\ninformation: \n\n### Kotlin\n\n```kotlin\nval response: ReferrerDetails = referrerClient.installReferrer\nval referrerUrl: String = response.installReferrer\nval referrerClickTime: Long = response.referrerClickTimestampSeconds\nval appInstallTime: Long = response.installBeginTimestampSeconds\nval instantExperienceLaunched: Boolean = response.googlePlayInstantParam\n```\n\n### Java\n\n```java\nReferrerDetails response = referrerClient.getInstallReferrer();\nString referrerUrl = response.getInstallReferrer();\nlong referrerClickTime = response.getReferrerClickTimestampSeconds();\nlong appInstallTime = response.getInstallBeginTimestampSeconds();\nboolean instantExperienceLaunched = response.getGooglePlayInstantParam();\n```\n\n**Caution:** The install referrer information will be\navailable for 90 days and **won't change** unless the application is\nreinstalled. To avoid unnecessary API calls in your app, you should invoke the\nAPI **only once** during the first execution after install.\n\nClosing service connection\n--------------------------\n\nAfter getting referrer information, call the\n[`endConnection()`](/reference/com/android/installreferrer/api/InstallReferrerClient#endConnection())\nmethod on your\n[`InstallReferrerClient`](/reference/com/android/installreferrer/api/InstallReferrerClient)\ninstance to close the connection. Closing the connection will help you avoid\nleaks and performance problems.\n\nFor further information, refer to the [Play Install Referrer Library\nReference](/reference/com/android/installreferrer/packages)."]]