Android kotlin google play billing onPurchasesUpdated 不覆盖任何内容或从未使用过

Kin*_*ing 2 java android billing kotlin

我正在按照此文档来实施 google pay:

https://developer.android.com/google/play/billing/billing_library_overview

运行此函数并购买产品后:

fun buy() {

    val skuList = listOf("vip_small")

    if (billingClient.isReady) {
        val params = SkuDetailsParams
                .newBuilder()
                .setSkusList(skuList)
                .setType(BillingClient.SkuType.INAPP)
                .build()
        billingClient.querySkuDetailsAsync(params) { responseCode, skuDetailsList ->

            if (responseCode == BillingClient.BillingResponse.OK) {

                Log.d("lets", "querySkuDetailsAsync, responseCode: $responseCode")


                val flowParams = BillingFlowParams.newBuilder()
                        .setSku("vip_small")
                        .setType(BillingClient.SkuType.INAPP) // SkuType.SUB for subscription
                        .build()

                val responseCodee = billingClient.launchBillingFlow(this, flowParams)

                Log.d("lets", "launchBillingFlow responseCode: $responseCodee")

            } else {
                Log.d("lets", "Can't querySkuDetailsAsync, responseCode: $responseCode")
            }
        }
    } else {
        Log.d("lets", "Billing Client not ready")
    }
}
Run Code Online (Sandbox Code Playgroud)

效果很好,我想知道是否已购买,所以我从鳕鱼中添加了以下代码:

override fun onPurchasesUpdated(@BillingResponse responseCode: Int, purchases: List<Purchase>?) {
    if (responseCode == BillingResponse.OK && purchases != null) {
        for (purchase in purchases) {
            handlePurchase(purchase)
        }
    } else if (responseCode == BillingResponse.USER_CANCELED) {
        // Handle an error caused by a user cancelling the purchase flow.
    } else {
        // Handle any other error codes.
    }
}
Run Code Online (Sandbox Code Playgroud)

但我得到了错误'onPurchasesUpdated' overrides nothing

所以我删除override并得到这个“错误”

Function "onPurchasesUpdated" is never used
Run Code Online (Sandbox Code Playgroud)

我勒个去??购买完成后如何调用这个该死的函数?

Kin*_*ing 6

我得到了解决方案。

您需要使活动/片段使用PurchasesUpdatedListener类似的:

class VIP : AppCompatActivity(), PurchasesUpdatedListener {

}
Run Code Online (Sandbox Code Playgroud)

然后覆盖就会起作用