您可以使用 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();
断开服务连接
获取引荐来源信息后,请对 InstallReferrerClient 实例调用 endConnection() 方法来断开连接。断开连接有助于避免出现泄露和性能问题。
如需了解详情,请参阅 Play Install Referrer 库参考文档。