android - 为什么 processPurchases() 在 play-billing-samples 中启动了两次?
问题描述
图 3 和代码 A 来自项目 play-billing-samples,您可以在此处查看。
从文档来看,launchBillingFlow
当用户想买东西时,可能会被一个按钮点击触发,然后onPurchasesUpdated
就会被启动。
我对override fun onBillingSetupFinished(billingResult: BillingResult){ }
, onBillingSetupFinished
(include 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
这可能是您在代码中看到两个调用的原因。
推荐阅读
- javascript - 将项目从 X 容器移动到 Y 容器 | Javascript
- javascript - 如何在codeigniter中使用输出jason将添加的数据保存到数据库中
- python - tKinter 和 cv2 GUI - 加载视频时使用 pyinstaller 和 freezez 编译
- xamarin - 无法获取 SfMaskedEdit 和 SfNumericTextBox 的值
- java - 如何制作“您正在使用 3G。连接到 WiFi”对话框?
- python-3.x - 在python中更新给定索引处的值时结果不同
- python - 当我在 Python 的另一个函数中使用它时,我可以隐藏一些函数的返回值吗?
- javascript - Node.js 可写流完成未在类中触发
- python-3.x - 如何使用 Python 显示 Flash 文件 (swf)
- python - 角色在 pygame 中卡住/冻结