简体   繁体   English

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

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

I'm following this docs to implement google pay: 我正在按照以下文档实施Google Pay:

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

After running this function and purchage a product: 运行此功能并购买产品后:

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

which works fine I want to know if the purchage has been made so I add this code from the 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.
    }
}

But I get the error 'onPurchasesUpdated' overrides nothing 但是我收到错误信息'onPurchasesUpdated' overrides nothing该错误'onPurchasesUpdated' overrides nothing

So I remove override and get this "error" 所以我删除了override并得到了这个“错误”

Function "onPurchasesUpdated" is never used

What the hell?? 我勒个去?? How to call this damn function after the purchage has been made? 购买后如何调用该死的函数?

I got the solution. 我找到了解决方案。

You need to make the activity/fragment use the PurchasesUpdatedListener like: 您需要使活动/片段使用PurchasesUpdatedListener例如:

class VIP : AppCompatActivity(), PurchasesUpdatedListener {

}

Then override will work 然后覆盖将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM