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

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

設定開發環境

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

後續步驟

測試應用程式的應用程式內審查流程,確認整合是否正常運作。