价格一开始不会显示给用户,需要重新打开活动几次才能显示价格或点击购买按钮。
billingClient = BillingClient.newBuilder(this)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build()
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
getPrice(PRODUCT_ID_1)
getPrice(PRODUCT_ID_2)
getPrice(PRODUCT_ID_3)
getPrice(PRODUCT_ID_4)
}
}
}
和函数 getPrice:
private fun getPrice(productId: String) {
// Define the product details to query
val productList =
listOf(
QueryProductDetailsParams.Product.newBuilder()
.setProductId(productId)
.setProductType(BillingClient.ProductType.INAPP)
.build(),
)
// Create query parameters
val params = QueryProductDetailsParams.newBuilder().setProductList(productList).build()
// Query product details
billingClient.queryProductDetailsAsync(params) { billingResult, productDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
if (productDetailsList.size > 0) {
// Extract the product details
val productDetails = productDetailsList[0]
// Get the price of the product
val price = productDetails.oneTimePurchaseOfferDetails?.formattedPrice
// Update the corresponding TextView with the price
when (productId) {
PRODUCT_ID_1 -> findViewById<TextView>(R.id.price1).text = price
PRODUCT_ID_2 -> findViewById<TextView>(R.id.price2).text = price
PRODUCT_ID_3 -> findViewById<TextView>(R.id.price3).text = price
PRODUCT_ID_4 -> findViewById<TextView>(R.id.price4).text = price
}
}
}
}
}
有什么想法为什么带有价格的文本视图通常不显示值吗?仅当我重新打开活动或单击购买按钮(将运行 launchBillingFlow)时才会出现。
您遇到的问题是,在重新打开活动或单击购买按钮之前,不会向用户显示价格,这可能是由于计费查询的异步性质造成的。在您的代码中,您使用 BillingClient 异步查询产品详细信息,并且在查询完成时更新 TextView。
为了确保在活动开始时向用户显示价格,您应该将使用价格更新 TextView 的代码移至 onBillingSetupFinished 回调。这可确保在设置计费客户端时获取并显示价格。以下是修改代码的方法:
billingClient = BillingClient.newBuilder(this)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build()
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
// Query and display prices when billing setup is finished
getPrice(PRODUCT_ID_1)
getPrice(PRODUCT_ID_2)
getPrice(PRODUCT_ID_3)
getPrice(PRODUCT_ID_4)
}
}
})
// Remove getPrice calls from here
private fun getPrice(productId: String) {
// Define the product details to query
val productList =
listOf(
QueryProductDetailsParams.Product.newBuilder()
.setProductId(productId)
.setProductType(BillingClient.ProductType.INAPP)
.build(),
)
// Create query parameters
val params = QueryProductDetailsParams.newBuilder().setProductList(productList).build()
// Query product details
billingClient.queryProductDetailsAsync(params) { billingResult, productDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
if (productDetailsList.size > 0) {
// Extract the product details
val productDetails = productDetailsList[0]
// Get the price of the product
val price = productDetails.oneTimePurchaseOfferDetails?.formattedPrice
// Update the corresponding TextView with the price
when (productId) {
PRODUCT_ID_1 -> findViewById<TextView>(R.id.price1).text = price
PRODUCT_ID_2 -> findViewById<TextView>(R.id.price2).text = price
PRODUCT_ID_3 -> findViewById<TextView>(R.id.price3).text = price
PRODUCT_ID_4 -> findViewById<TextView>(R.id.price4).text = price
}
}
}
}
}
通过将 getPrice 调用移至 onBillingSetupFinished 回调,您可以确保在设置计费客户端后立即获取并显示价格,而不是依赖用户交互来触发查询。这应该可以解决在重新开放活动或单击购买按钮之前不显示价格的问题。