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

在文档中,当用户想购买商品时,可以通过单击按钮触发launchBillingFlow,然后启动onPurchasesUpdated

我对override fun onBillingSetupFinished(billingResult: BillingResult){ }的注释感到困惑,当onBillingSetupFinished成功建立后,就会启动processPurchases()(包括BillingClient)。

通常情况下,我需要先初始化BillingClient,启动onBillingSetupFinished,也启动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)
   }

最佳答案

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

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

关于android - 为什么在游戏计费样本中两次启动流程Purchases()?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61926353/

10-12 00:33
查看更多