为什么 processPurchases() 在 play-billing-samples 中启动了两次?

Why is processPurchases() launuched two times in play-billing-samples?

图3和代码A来自项目play-billing-samples,可以看到here.

从文档中可以看出,launchBillingFlow可能是用户想买东西时点击按钮触发的,然后onPurchasesUpdated就会启动。

我对override fun onBillingSetupFinished(billingResult: BillingResult){ }的评论感到困惑,onBillingSetupFinished(包括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)
   }

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

当然,当您单击 "buy" 按钮时,也会立即调用 processPurchases。这可能是您在代码中看到两次调用 processPurchases 的原因。