Play Install Referrer 库
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以使用 Google Play 商店的 Install Referrer API 从 Google Play 安全地检索引荐来源内容。Play Install Referrer API 客户端库采用 Java 编程语言编写,是用于定义 Install Referrer 服务接口的 Android 接口定义语言 (AIDL) 文件的封装容器。您可以使用 Play Install Referrer API 客户端库简化开发过程。
本指南介绍了使用 Play Install Referrer 库从 Google Play 检索引荐来源信息的基础知识。
更新应用的依赖项
将以下代码行添加到应用的 build.gradle
文件的 dependencies 部分:
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 库客户端连接可能会中断。在发送进一步的请求之前,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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):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)."]]