Android Kotlin Google Play Billing onPurchasesUpdated会覆盖所有内容或从不使用

时间:2019-02-01 00:20:46

标签: java android kotlin billing

我正在关注此文档以实施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")
    }
}

工作正常,我想知道是否已购买商品,所以我从鳕鱼中添加了以下代码:

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并得到了这个“错误”

Function "onPurchasesUpdated" is never used

什么鬼?购买后如何调用该死的功能?

1 个答案:

答案 0 :(得分:0)

我找到了解决方法。

您需要使活动/片段使用PurchasesUpdatedListener,例如:

class VIP : AppCompatActivity(), PurchasesUpdatedListener {

}

然后覆盖将起作用