您可以使用 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 Install Referrer API 库之前,您必须按照以下步骤与 Play 商店应用建立连接:
- 调用
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 库参考。