本指南介绍了如何使用 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. });
后续步骤
测试应用的应用内评价流程 以验证您的集成是否正常工作。