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

v64noz0r  于 2023-05-27  发布在  Android
关注(0)|答案(2)|浏览(114)

图3和代码A是来自play-billing-samples项目,可以看到here
从文档中可以看出,当用户想要购买东西时,可以通过点击按钮来触发launchBillingFlow,然后onPurchasesUpdated就会启动。
我对override fun onBillingSetupFinished(billingResult: BillingResult){ }的评论感到困惑,onBillingSetupFinished(包括processPurchases())将在BillingClient成功建立后启动。
通常情况下,我需要首先初始化BillingClientonBillingSetupFinished被启动,processPurchases()也会被启动,然后我点击一个 Buy 按钮,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)
   }
bakd9h0s

bakd9h0s1#

每次在您单击“购买”按钮之前建立与Google Play BillingClient的新连接时,都会调用processPurchases(从queryPurchasesAsync()内部调用)以处理任何已购买但尚未消费的过去购买。这可能是因为间歇性的互联网连接中断或其他原因。您可以将此processPurchases调用视为完成和消费以前的购买(如果有)。
当然,当您单击“buy”按钮时,processPurchases也会立即被调用。这可能就是为什么在代码中看到两个对processPurchases的调用的原因。

jdzmm42g

jdzmm42g2#

在我的例子中,我没有正确地发布billingClient。因此,如果我退出该活动并再次打开它,将导致此问题。下面的代码解决了我的问题,

@Override
    protected void onDestroy() {
        if (billingClient != null) {
            billingClient.endConnection();
            billingClient = null;
        }
        super.onDestroy();
    }

相关问题