整合應用程式內評論 (Kotlin 或 Java)

本指南說明如何使用 Kotlin 或 Java,將應用程式內評論整合到您的應用程式中。如果您使用原生程式碼Unity,請參考專用的整合指南。

設定開發環境

Play 應用程式內評論程式庫是 Google Play Core Library 的一部分。請加入以下 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.2'

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

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.2")

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

建立 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.
});

後續步驟

測試應用程式內評論流程,以確認這項整合機制能正常運作。