首页 > 解决方案 > 为什么 processPurchases() 在 play-billing-samples 中启动了两次?

问题描述

图 3 和代码 A 来自项目 play-billing-samples,您可以在此处查看

从文档来看,launchBillingFlow当用户想买东西时,可能会被一个按钮点击触发,然后onPurchasesUpdated就会被启动。

我对override fun onBillingSetupFinished(billingResult: BillingResult){ }, onBillingSetupFinished(include processPurchases()) 的评论感到困惑,将BillingClient在成功建立后启动。

通常情况下,我需要先初始化BillingClientonBillingSetupFinished被启动,processPurchases()也将被启动,然后我点击购买按钮,onPurchasesUpdated将被启动,然后processPurchases()再次启动。

代码有问题吗?

* Figure 3 -- Server-reliant billing integration with offline access to some entitlements
 *
 *  _____                        _________________
 * |Start|----------------------|launchBillingFlow|
 *  -----                        -----------------
 *                                        |
 *                                  ______v____________
 *                                 |onPurchasesUpdated |
 *                                  -------------------
 *                                 /      |
 *                   ITEM_ALREADY_OWNED   |
 *                               /        |
 *  _____       ________________v__       |
 * |Start|-----|queryPurchasesAsync|      OK
 *  -----       -------------------       |
 *                               \        |
 *                               v________v_______
 *                              |processPurchases |
 *                               -----------------
 *                                        |
 *   

                                 |

代码 A

    /**
     * This is the callback for when the connection to the Play [BillingClient] has been successfully
     * established. It might make sense to get [SkuDetails] and [Purchases][Purchase] at this point.
     */
    override fun onBillingSetupFinished(billingResult: BillingResult) {
        when (billingResult.responseCode) {
            BillingClient.BillingResponseCode.OK -> {
                Log.d(LOG_TAG, "onBillingSetupFinished successfully")
                querySkuDetailsAsync(BillingClient.SkuType.INAPP, GameSku.INAPP_SKUS)
                querySkuDetailsAsync(BillingClient.SkuType.SUBS, GameSku.SUBS_SKUS)
                queryPurchasesAsync()
            }
            BillingClient.BillingResponseCode.BILLING_UNAVAILABLE -> {
                //Some apps may choose to make decisions based on this knowledge.
                Log.d(LOG_TAG, billingResult.debugMessage)
            }
            else -> {
                //do nothing. Someone else will connect it through retry policy.
                //May choose to send to server though
                Log.d(LOG_TAG, billingResult.debugMessage)
            }
        }
    }



    override fun onPurchasesUpdated(
            billingResult: BillingResult,
            purchases: MutableList<Purchase>?
    ) {
        when (billingResult.responseCode) {
            BillingClient.BillingResponseCode.OK -> {
                // will handle server verification, consumables, and updating the local cache
                purchases?.apply { processPurchases(this.toSet()) }
            }
            BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED -> {
                // item already owned? call queryPurchasesAsync to verify and process all such items
                Log.d(LOG_TAG, billingResult.debugMessage)
                queryPurchasesAsync()
            }
            BillingClient.BillingResponseCode.SERVICE_DISCONNECTED -> {
                connectToPlayBillingService()
            }
            else -> {
                Log.i(LOG_TAG, billingResult.debugMessage)
            }
        }
    }


   fun queryPurchasesAsync() {
       ...
       processPurchases(purchasesResult)
   }

标签: androidgoogle-play

解决方案


每次在您单击“购买”按钮之前建立与 Google Play BillingClient 的新连接时,processPurchases都会调用(从内部queryPurchasesAsync()处理任何已购买但尚未消费的过去购买。这可能由于间歇性互联网连接中断或其他原因而发生. 您可以将此processPurchases调用视为完成并消耗之前的购买(如果有的话)。

自然,当您单击“购买”按钮时,processPurchases也会立即调用。processPurchases这可能是您在代码中看到两个调用的原因。


推荐阅读