集成应用内评价(Kotlin 或 Java)

本指南介绍如何使用 Kotlin 或 Java 将应用内评价集成到您的应用中。如果您使用的是原生代码Unity,请参阅单独的集成指南。

设置您的开发环境

Play 应用内评价库是 Google Play Core 库的一部分。请添加以下 Gradle 依赖项,以集成 Play 应用内评价库。

Groovy

// In your app’s build.gradle file:
...
dependencies {
    // This dependency is downloaded from the Google’s Maven repository.
    // So, make sure you also include that repository in your project's build.gradle file.
    implementation 'com.google.android.play:review:2.0.1'

    // For Kotlin users also add the Kotlin extensions library for Play In-App Review:
    implementation 'com.google.android.play:review-ktx:2.0.1'
    ...
}

Kotlin

// In your app’s build.gradle.kts file:
...
dependencies {
    // This dependency is downloaded from the Google’s Maven repository.
    // So, make sure you also include that repository in your project's build.gradle file.
    implementation("com.google.android.play:review:2.0.1")

    // For Kotlin users also import the Kotlin extensions library for Play In-App Review:
    implementation("com.google.android.play:review-ktx:2.0.1")
    ...
}

创建 ReviewManager

ReviewManager 界面可让您的应用启动应用内评价流程。使用 ReviewManagerFactory 创建实例获取该界面。

Kotlin

val manager = ReviewManagerFactory.create(context)

Java

ReviewManager manager = ReviewManagerFactory.create(context)

请求 ReviewInfo 对象

请遵循有关何时请求应用内评价的指南,以确定在应用的用户流的哪些阶段适合提示用户进行评价(例如,当用户在游戏中完成某个关卡时)。当您的应用达到其中一个阶段时,请使用 ReviewManager 实例创建请求任务。如果请求成功,该 API 将返回启动应用内评价流程所需的 ReviewInfo 对象。

Kotlin

val request = manager.requestReviewFlow()
request.addOnCompleteListener { task ->
    if (task.isSuccessful) {
        // We got the ReviewInfo object
        val reviewInfo = task.result
    } else {
        // There was some problem, log or handle the error code.
        @ReviewErrorCode val reviewErrorCode = (task.getException() as ReviewException).errorCode
    }
}

Java

ReviewManager manager = ReviewManagerFactory.create(this);
Task<ReviewInfo> request = manager.requestReviewFlow();
request.addOnCompleteListener(task -> {
    if (task.isSuccessful()) {
        // We can get the ReviewInfo object
        ReviewInfo reviewInfo = task.getResult();
    } else {
        // There was some problem, log or handle the error code.
        @ReviewErrorCode int reviewErrorCode = ((ReviewException) task.getException()).getErrorCode();
    }
});

启动应用内评价流程

使用 ReviewInfo 实例启动应用内评价流程。等到用户完成应用内评价流程后,再继续执行应用的正常用户流(例如进入下一关)。

Kotlin

val flow = manager.launchReviewFlow(activity, reviewInfo)
flow.addOnCompleteListener { _ ->
    // The flow has finished. The API does not indicate whether the user
    // reviewed or not, or even whether the review dialog was shown. Thus, no
    // matter the result, we continue our app flow.
}

Java

Task<Void> flow = manager.launchReviewFlow(activity, reviewInfo);
flow.addOnCompleteListener(task -> {
    // The flow has finished. The API does not indicate whether the user
    // reviewed or not, or even whether the review dialog was shown. Thus, no
    // matter the result, we continue our app flow.
});

后续步骤

测试应用的应用内评价流程,以验证您的集成是否正常运行。