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

Android kotlin google play billing onPurchasesUpdated overrides nothing or is never used

我正在关注此文档以实施 google 支付:

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")
    }
}

效果很好我想知道是否已购买,所以我从 cods 添加此代码:

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

但是我收到错误 'onPurchasesUpdated' overrides nothing

所以我删除 override 并得到这个 "error"

Function "onPurchasesUpdated" is never used

什么鬼??购买完成后如何调用这个该死的函数?

我找到了解决方案。

您需要让 activity/fragment 使用 PurchasesUpdatedListener,例如:

class VIP : AppCompatActivity(), PurchasesUpdatedListener {

}

然后覆盖将起作用